Tuesday, 24 June 2014

6.BAPI : Events of Business Object -> Raising & Handling Events

Scenario: Usually when a BAPI method is called and some Business Object is created/changed like Ex- if New Employee is created or  changed the status should reflect in some other SAP system or Non SAP system. So  this can be achieved by Event technique of the Business Object and when a event is triggered either the registered function module or Method is called to send the status back to the receiver system.

The below post describes the creation of BO method and its event and how to register the event with the handle function module.

------------------------------------------------------------------------------------------------------------------------------


Step1. Create the below table with following fields.




















Step2. For the Data Element : ZCREATED_VIA, create a domain ZCREATED_VIA and maintain two domain fixed values.

















Step3. Create some table entries.













Step4. Go to Tx: SWO1 & create a Business Object- ZEMP_3000.





















Step5. Provide the details & click on continue button.















Step6. Select the method tab and click on Create button.


















Step7. Click on No button as we are not going to refer to any Function Module.


















Step8. Provide details & click on Continue button.






















  


Step9. Select the newly created method & click on Parameters button.





















Step10. Click on Create button.















Step11. Click on NO button.




















Step12. The method needs the details of the Employee to create it except the Information about field : CREATED_VIA bcoz this field should be filled in the Event handler Function module.






























Step13. One importing structure is defined . This method exports the Employee Number after successful creation. So click on the Create button to create an exporting parameter.

















Step14. Click on NO button.





















Step15. Provide the details and continue.






















Step16. Two parameters are defined for the method.


















Step17. Select the method and click on Program button.
























Step18. Click on YES button.



















Step19. Insert some codes like insert query and others as highlighted in the program & go Back.



























Step20. Navigate along the menu path and Implement the Business Object and then Release the business object.




















Step21. Select our method and then from the menu path Implement the method and then release the method.




















Step22. Up to this our Method and program is created. We have to create a event. So select the Event option and click on Create button.



















Step23. Provide the event name & other details. Then continue.






















Step24. Follow the menu path to implement and then release the event.























Step25. When the event is triggered, we ll pass some parameters. So select the event name and click on the Parameters button.






















Step26. Click on Create button.

















Step27. Click on NO button.






















Step29. Provide the name and other details. This event receives the EMP_ID as a parameter.
























Step30. Go Back.

















Step31. Generate the Business Object by clicking on the Generate button.























Step32. Let's test the method. So select the method and click on the Test button.
























Step33. Click on Execute button.





















Step34. Now we need to fill the importing structure of the Method. So click on the highlighted button.















Step35. Fill the employee details to be created and go back.













Step36. Now execute the method.

















Step37. We receive the employee id '0003' as export parameter.



















Step38. Check in the table& the Employee 0003 is created.














Step39. Execute the report.  Code given in step 40.



















---------------------------------------------------------------------------------------------------------------------------------


Step40.

REPORT  zbapi_bo_call.
DATA : objtype        TYPE swotobjid-objtype,
              objkey         TYPE swotobjid-objkey,
              object          TYPE swotrtime-object,
              lt_cont         TYPE TABLE OF swcont,
              ls_cont         TYPE swcont,
              return          TYPE  swotreturn,
             verb             TYPE swotinvoke-verb,
             ls_emp        TYPE zemp_his,
             emp_id        TYPE zemp_his-emp_id,
             event_id       TYPE swedumevid-evtid.

objtype = 'ZEMP_3000'. " Business Object Type Name
objkey  = 'EMPLOYEE'.
verb = 'CRETAE_EMPLOYEE'. "Method Name to be Invoked

ls_emp-emp_id = '0004'.
ls_emp-name = 'Developer4'.
ls_emp-design = 'Manager'.
ls_emp-dept = 'Csutom Supp'.
ls_emp-salary = '500000.00'.
ls_emp-sal_unit = 'INR'.

CALL FUNCTION 'SWO_CREATE'
  EXPORTING
    objtype = objtype
    objkey  = objkey
  IMPORTING
    object  = object
    return  = return.

CALL FUNCTION 'SWC_ELEMENT_SET'

  EXPORTING
    element   = 'EMP_INFO'
    field     = ls_emp
  TABLES
    container = lt_cont.

CALL FUNCTION 'SWO_INVOKE'

  EXPORTING
    access    = 'C'  " C- to call method
    object    = object
    verb      = verb
  TABLES
    container = lt_cont.

IF sy-subrc = 0.
  LOOP AT lt_cont INTO ls_cont WHERE element = 'NEW_EMP_ID'.
    IF emp_id IS INITIAL.
      emp_id = ls_cont-value.
      IF emp_id IS NOT INITIAL.
        EXIT.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDIF.
WRITE :/ 'New Employee Created ', emp_id.
---------------------------------------------------------------------------------------------------------------------------------



Step41. After executing the program one more Employee '0004' is created successfully by BO program.
















Step41. In the table the Employee '0004' is available.















Step42. When any BO method is executed, it would trigger an event and there should be either any Function Module or Method which should handle it. Let's create a Function Module.
Create a Function module and it should be a Remote Enabled.






















Step43. Fill these importing parameters.

















Step44. Provide these export parameters.














Step45. Provide these parameters under the Tables option.


















Step46. Provide the below code. The objective is BO method will create a Employee record without filling some field here like CREATED_VIA field and will trigger event for which a FM will be called and it would filled the field CREATED_VIA.
























Step47. As this FM is a Remote Enable, we need to create a RFC destination which we need in our later customizing setting. Here as this event triggers a FM in the same system it doesn't make much sense to have this RFC but in a real time environment it may require we need to pass the information to a different system.
So go to Tx: SM59 and create a Logical Connection.














Step48. Provide the below details and Save it.






























Step49. No go to Tx: SWE2 and and click on New Entries button.












Step50. Provide the BO name, event name, Receiver type we can put any name. Receiver FM provide the above created Function module and Destination as our RFC Destination in SM59. Save it.




























-------------------------------------------------------------------------------------------------------------------------------


Step51. Again modify the report as per given below to raise the event.

REPORT  zbapi_bo_call.
DATA : objtype       TYPE swotobjid-objtype,
              objkey        TYPE swotobjid-objkey,
              object         TYPE swotrtime-object,
              lt_cont        TYPE TABLE OF swcont,
              ls_cont       TYPE swcont,
              return         TYPE  swotreturn,
              verb           TYPE swotinvoke-verb,
             ls_emp        TYPE zemp_his,
             emp_id       TYPE zemp_his-emp_id,
             event_id     TYPE swedumevid-evtid.

objtype = 'ZEMP_3000'. " Business Object Type Name

objkey  = 'EMPLOYEE'.
verb = 'CREATE_EMPLOYEE'. "Method Name to be Invoked

ls_emp-emp_id = '0005'.
ls_emp-name = 'Developer 5'.
ls_emp-design = 'Principal Engg'.
ls_emp-dept = 'Csutom Supp'.
ls_emp-salary = '800000.00'.
ls_emp-sal_unit = 'INR'.

CALL FUNCTION 'SWO_CREATE'
  EXPORTING
    objtype = objtype
    objkey  = objkey
  IMPORTING
    object  = object
    return  = return.

CALL FUNCTION 'SWC_ELEMENT_SET'  " SET THE INPUT PARAMETER FOR THE METHOD
  EXPORTING
    element   = 'EMP_INFO'
    field     = ls_emp
  TABLES
    container = lt_cont.

CALL FUNCTION 'SWO_INVOKE'
  EXPORTING
    access    = 'C'  " C- to call method
    object    = object
    verb      = verb
  TABLES
    container = lt_cont.

IF sy-subrc = 0.
  LOOP AT lt_cont INTO ls_cont WHERE element = 'NEW_EMP_ID'.
    IF emp_id IS INITIAL.
      emp_id = ls_cont-value.
      IF emp_id IS NOT INITIAL.
        EXIT.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDIF.
CALL FUNCTION 'SWC_ELEMENT_SET' " SET THE PARAMETER FOR THE EVENT
  EXPORTING
    element   = 'EMPLOYEE_ID'
    field     = emp_id
  TABLES
    container = lt_cont.

CALL FUNCTION 'SWE_EVENT_CREATE' " RAISE THE EVENT
  EXPORTING
    objtype         = objtype
    objkey          = objkey
    event           = 'EMP_CRT_SUCC' " EVENT NAME
  IMPORTING
    event_id        = event_id
  TABLES
    event_container = lt_cont.
IF sy-subrc = 0.
  COMMIT WORK. " Invoke the call of the registered FM for the Event
ENDIF.

WRITE :/ 'New Employee Created ', emp_id, '&', 'Event is triggered '.
------------------------------------------------------------------------------------------------------------------------------


Step52. Execute the report.




























Step53. Just put a debug point in the Event Handler Function module. We arrived here. So the Event is triggered and it called the FM.


























Step54. The report O/P is displayed.
















Step55. Check in the table.












---------------------------------------xxxxxxxxxxxx---------------------------------------------------------------------------

No comments:

Comments system

Disqus Shortname