Working with Business Objects in SAP

...Previous

Now lets see how to call a method of BO. The Method DISPLAY will display the material passed in BO container.

*&---------------------------------------------------------------*
*& Report  ZSWET_BO1
*&
*&---------------------------------------------------------------*
*& To call method with import parameters
*&
*&---------------------------------------------------------------*
REPORT  zswet_bo1.
* Get an attribute of a business object.
PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006',
            p_key(70) TYPE c DEFAULT 'ZSHUKSWE20' ,
            p_attr(32) TYPE c DEFAULT 'DISPLAY',
            p_access TYPE c DEFAULT 'C'. "To call method put 'C'
DATA:
    i_objtype TYPE swo_objtyp,
    i_objkey TYPE swo_typeid,
    i_element TYPE swo_verb.
DATA object TYPE swo_objhnd.
DATA verb TYPE swo_verb.
DATA return TYPE swotreturn.
DATA lt_container TYPE STANDARD TABLE OF swcont.
i_objtype = p_busobj.
i_element = p_attr.
i_objkey = p_key.
*To call the method we need to populate the Container of the
*BO with importing parameters
CALL FUNCTION 'SWC_ELEMENT_SET'
  EXPORTING
    element   = 'MATERIAL'
    field     = p_key(18)    "Material Number
  TABLES
    container = lt_container
  EXCEPTIONS
    OTHERS    = 1.
*Instantiate the business object. I.e give it a key and create it.
CALL FUNCTION 'SWO_CREATE'
  EXPORTING
    objtype = i_objtype
    objkey  = i_objkey
  IMPORTING
    object  = object.
* To call the Method of the BO.
CALL FUNCTION 'SWO_INVOKE'
  EXPORTING
    access    = p_access
    object    = object
    verb      = i_element
  IMPORTING
    return    = return
    verb      = verb
  TABLES
    container = lt_container.

The Output will be:- 

                        

Lets see an example below on How  to Call the methods using the macros defined in include <CNTN01>. 

*&----------------------------------------------------------------*
*& Report  ZSWET_BO1
*&
*&----------------------------------------------------------------*
*& Using macros defined in CNTN01 include. To call method of BO
*& For reference check FM DNO_DB_APPENDIX_INSERT
*&----------------------------------------------------------------*
REPORT  zswet_bo1.
INCLUDE <cntn01>.
* Get an attribute of a business object.
PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006',
            p_key(70) TYPE c DEFAULT 'ZSHUKSWE20',
            p_attr(32) TYPE c DEFAULT 'DISPLAY',
            p_access TYPE c DEFAULT 'C'. "To call method put 'C'
DATA:
    i_objtype TYPE swo_objtyp,
    i_objkey TYPE swo_typeid,
    i_element TYPE swo_verb.
DATA: gv_mat TYPE swc_object.
i_objtype = p_busobj.
i_element = p_attr.
i_objkey = p_key.
*Define container
swc_container container.
*Create instance of Object type
swc_create_object gv_mat i_objtype i_objkey.
**gv_mat will contain instance of the BO
*To clear a continer
swc_clear_container container.
*Now to call a method fill up container with import parameters for method
swc_set_element container 'MATERIAL' p_key(18). "'MATERIAL' ->import parameter
*For container of type table use swc_set_table.
***If any more import parameter are there for the object then populate them
***also using swc_set_element and swc_set_table
*In this case no more import parameters
*To call a method stored in p_attr
swc_call_method gv_mat p_attr container.
*If there are any export parameter, then CONTAINER will have the values
*and we can read from container

The Output will be:- 

                        

1.4          To trigger an Event of BO 

We can use the FM ‘SWE_EVENT_CREATE’  to raise an event in any report program. An explicit commit work is required after the FM call to trigger the event, other wise event will not be raised. 

*&---------------------------------------------------------------*
*& Report  ZSWET_BO1
*&
*&---------------------------------------------------------------*
REPORT  zswet_bo1.
INCLUDE <cntn01>.
* Get an attribute of a business object.
PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006',
            p_key(70) TYPE c DEFAULT 'ZSHUKSWE20'.
DATA:
    i_objtype TYPE swo_objtyp,
    i_objkey TYPE swo_typeid.
i_objtype = p_busobj.
i_objkey = p_key.
*Define container
swc_container container.
*To clear a continer
swc_clear_container container.
*You can populate the Container if required
*To generate event
CALL FUNCTION 'SWE_EVENT_CREATE'
  EXPORTING
    objtype           = i_objtype
    objkey            = i_objkey
    event             = 'CREATED'
  TABLES
    event_container   = container
  EXCEPTIONS
    objtype_not_found = 1
    OTHERS            = 2.
IF sy-subrc <> 0.
  " MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  "         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4
  "         RAISING OBJTYPE_NOT_FOUND.
ENDIF.
COMMIT WORK.

 

Just triggering an event does not makes any sense until and unless a proper event receiver id is linked to the event. This event receiver can be a FM or can be any workflow. Event linkage can be done in SWE2 or SWETYPV transactions.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image