Working with Business Objects in SAP

...Previous

Save the method and then Click on the Program button to implement the method. In the method just read all ebeln (Purchase line items) corresponding to PO number into internal table ITEM and pass it on to container in the method. Check the snapshot below. 

 

Check the whole code of the BO program here.  

*****           Implementation of object type ZSWE1                *****
INCLUDE <OBJECT>.
BEGIN_DATA OBJECT. " Do not change.. DATA is generated
* only private members may be inserted into structure private
DATA:
" begin of private,
"   to declare private attributes remove comments and
"   insert private attributes here ...
" end of private,
  BEGIN OF KEY,
      PURCHASINGDOCUMENT LIKE EKKO-EBELN,
  END OF KEY.
END_DATA OBJECT. " Do not change.. DATA is generated
BEGIN_METHOD READ CHANGING CONTAINER.
DATA:
      PURCHASINGDOCUMENT TYPE EKKO-EBELN,
      ITEM TYPE EKPO-EBELP OCCURS 0.
  SWC_GET_ELEMENT CONTAINER 'PurchasingDocument' PURCHASINGDOCUMENT.
  select ebelp into table item from ekpo
  where ebeln = PURCHASINGDOCUMENT.
  SWC_SET_TABLE CONTAINER 'Item' ITEM.
END_METHOD. 

Step 1 : Save the changes and make the status of the BO to implemented for testing purpose. Also generate the BO. 

Step 2 : Now let’s test the BO. For this put a break point in the method READ at the select query. Also take any PO number which will have multiple line items. You can check EKKO and EKPO tables. In my case I am taking PO ‘4200000017’. In my R3 system this particular PO contains 4 line items in EKPO table. Let’s put a break point and check the method. 

 

Step 3 : Execute the BO with key ‘4200000017’ 

 

Execute the method with import parameter as ‘420000017’ 

 

It will take you to debugging mode. Now check the container. 

In debug mode before the select statement the CONTAINER will hold only import variable 

 

Now execute the last statement in the method and see the contents of CONTAINER (just before exiting the method. 

 

So we have seen how the CONTAINER holds values while we are working with Business Objects at runtime. 

1.2.2          Some common macros defined in include <CNTN01> for working with Containers 

Functionality

Macro

To write a single line variable in Container

SWC_SET_ELEMENT

To read a single line variable from Container

SWC_GET_ELEMENT

To write a multi line variable or internal table in Container

SWC_SET_TABLE

To read a multi line variable or internal table from Container

SWC_GET_TABLE

To clear the container

SWC_CLEAR_CONTAINER

Further you can check the include <CNTN01> for more macros. Also check the macros. These macros call some specific function modules that you can use directly in your code. 

1.3          Calling a BO Method/Attribute in report programs 

We can create a instance of BO method using FM  'SWO_CREATE' or macro ‘SWC_CREATE_OBJECT’. 

First of all lets consider a BO 'BUS1001006'  (StandardMaterial). We will call the method ‘DISPLAY’. For this we will instantiate the BO with key ‘ZSHUKSWE20’( material number). 

To call a method or attribute of any BO we can use the FM 'SWO_INVOKE'. We have to take care while we call this FM. Suppose if we want to call an attribute defined in the method, then we need to populate the import parameter ACCESS with ‘G’. If we need to call the method of the BO then we need to populate the import parameter ACCESS with ‘C’. 

Lets create a report program and check step by step. We will fetch details of attribute “MATERIALTYPE” of BO 'BUS1001006' 

*&---------------------------------------------------------------------*
*& Report  ZSWET_BO1
*&
*&---------------------------------------------------------------------*
*& To get attributes of BO instance in report
*&
*&---------------------------------------------------------------------*
REPORT  zswet_bo1.
PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006',
            p_key(70) TYPE c DEFAULT 'ZSHUKSWE20' ,
            p_attr(32) TYPE c DEFAULT 'MATERIALTYPE',
            p_access TYPE c DEFAULT 'G'. "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.
DATA line TYPE swcont.
i_objtype = p_busobj.
i_element = p_attr.
i_objkey = p_key.
*  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.
* Return attribute.
CALL FUNCTION 'SWO_INVOKE'
  EXPORTING
    access    = p_access
    object    = object
    verb      = i_element
  IMPORTING
    return    = return
    verb      = verb
  TABLES
    container = lt_container.
* The attribute value is in the container returned from FM.
IF return-code = 0.
  LOOP AT lt_container INTO line.
    WRITE: / 'Attribute MATERIAL TYPE is : ',
              line-value.
  ENDLOOP.
ENDIF. 

Lets execute the report and see the output: 

Click here to continue...

 

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