You are on page 1of 22

Working with Business Objects in SAP

By Swetabh Shukla, Infosys
Business Object type contains methods, attributes and events which give a component
based view of any Business Process.
For example we have a Purchase Order business process. This process includes various small
functionalities. It will have process of changing Purchase orders based upon requirements,
functionality to retrieve the details of purchase order etc. Each of these functionalities is
encapsulated inside a method. The method can call the function modules present in R3
system or can have there own code to execute some functionality. So if we consider
Purchase Order as a Business Object then it will be identified by key field Purchase Order
number. Each purchase order business object based upon key field purchase order number is
different. It’s so because each purchase order is different and will contain different details.
So the methods contain business functionality. Attributes are just like properties of the
Business object.For example for any purchase order Purchasing Group, Purchasing
Organization etc are attributes.
Technically we can say that business object types are just like any template. At runtime we
instantiate or create runtime objects for any BO (Business Object) type based upon the key
fields we pass. Two runtime Business objects of same BO type are different from each other
based upon the key fields we pass. So the key field is the differentiating factor for two or
more runtime business objects of same BO type.
To Browse for BO open tcode SWO2 (Path -> Tools ->ABAP Workbench->Overview->Business
Object Browser).

You can expand the nodes and can check various business objects in BOR. You can double
click on the BO node and it will take you to BO display (SWO1).
1Working with Business Object in our programs
For creating a Business Object and its attributes there are many already existing tutorials.So
in this tutorial we will not discuss creation of BO and its attributes.
1.1To create instance of a BO
Tags: Create, BO instance, key fields, FM SWO_CREATE, macro SWC_CREATE_OBJECT,
<cntn01>
To create an instance of BO we need to have the key fields of that BO. Key fields will be used
to uniquely identify the BO. We can use the FM 'SWO_CREATE' to create an instance of BO
in any report program.
DATA:
i_objtype TYPE swo_objtyp,
i_objkey TYPE swo_typeid,
object TYPE swo_objhnd.
i_objtype = <Business Object>

The other way is to use the macros ‘SWC_CREATE_OBJECT’ defined in include <cntn01> INCLUDE <cntn01>. 1. Business Object container is technically of type SWCONT structure. DATA: i_objtype TYPE swo_objtyp. i_objkey TYPE swo_typeid. So you can either go for direct FM call or the macro call. *Create instance of Object type swc_create_object object i_objtype i_objkey. Internally the macro calls the FM SWO_CREATE.i_objkey = <BO key> CALL FUNCTION 'SWO_CREATE' EXPORTING objtype = i_objtype objkey = i_objkey IMPORTING object = object. object TYPE swc_object. The variable ‘object’ will hold runtime instance of the object type. When ever we are calling any method of Business Object we need to populate container for import parameters and after the method gets executed it returns the values (export parameters) in Container. The Container actually holds import and export parameters associated with any method of Business Object at runtime. .2Container Basically the term Container is used with reference to Business Objects and Workflows.

Step 1 : Create BO ZSWE1 for test purpose. You can give it any name.1Lets take an example to check what Container is For example let’s create a Business Object ‘ZSWE1’ with only one method ‘READ’. We will discuss the creation of BO in very brief. 1. Key field will be Purchase Order number (EKKO_EBELN) for BO. the container will hold multiple values with same element name. Method READ will take Purchase Order number as input and will give all the Purchase Line items in it in an internal table. Step 2 : Now create a method READ .Here ‘ELEMENT’ will be name of the variable/internal table which container holds and ‘VALUE’ will have corresponding value.2. For multi line variable or internal tables.

Save the method. Now we will create the parameters of the method. . Import Parameters Export Parameter PurchaseDocument EKKO-EBELN EKKO-EBELP (multi line) PurchasingDocument will be a import parameter of type EKKO_EBELN.

ITEM will be a export parameter of type EKKO_EBELP and will be a multi line variable(internal table ) .

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. .Save the method and then Click on the Program button to implement the method. Check the snapshot below.

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

Let’s put a break point and check the method.where ebeln = PURCHASINGDOCUMENT. Step 3 : Execute the BO with key ‘4200000017’ . END_METHOD. Also generate the BO. Step 2 : Now let’s test the BO. You can check EKKO and EKPO tables. In my case I am taking PO ‘4200000017’. Step 1 : Save the changes and make the status of the BO to implemented for testing purpose. For this put a break point in the method READ at the select query. In my R3 system this particular PO contains 4 line items in EKPO table. Also take any PO number which will have multiple line items. SWC_SET_TABLE CONTAINER 'Item' ITEM.

Now check the container.Execute the method with import parameter as ‘420000017’ It will take you to debugging mode. In debug mode before the select statement the CONTAINER will hold only import variable .

1. So we have seen how the CONTAINER holds values while we are working with Business Objects at runtime.2.Some common macros defined in include <CNTN01> for working with Containers .Now execute the last statement in the method and see the contents of CONTAINER (just before exiting the method.

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’. "To call method put 'C' DATA: . p_attr(32) TYPE c DEFAULT 'MATERIALTYPE'. We will call the method ‘DISPLAY’. then we need to populate the import parameter ACCESS with ‘G’. p_key(70) TYPE c DEFAULT 'ZSHUKSWE20' . These macros call some specific function modules that you can use directly in your code. We have to take care while we call this FM. Lets create a report program and check step by step.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. Suppose if we want to call an attribute defined in the method. For this we will instantiate the BO with key ‘ZSHUKSWE20’( material number). p_access TYPE c DEFAULT 'G'. If we need to call the method of the BO then we need to populate the import parameter ACCESS with ‘C’. 1. Also check the macros. To call a method or attribute of any BO we can use the FM 'SWO_INVOKE'. First of all lets consider a BO 'BUS1001006'(StandardMaterial). 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'.

DATA verb TYPE swo_verb. CALL FUNCTION 'SWO_CREATE' EXPORTING objtype = i_objtype objkey = i_objkey IMPORTING object = object. CALL FUNCTION 'SWO_INVOKE' EXPORTING access = p_access object = object verb = i_element IMPORTING return = return verb = verb TABLES container = lt_container.e give it a key and create it. i_objkey TYPE swo_typeid. i. DATA lt_container TYPE STANDARD TABLE OF swcont. * Return attribute. * The attribute value is in the container returned from FM. DATA line TYPE swcont. i_objkey = p_key. i_element = p_attr. line-value. WRITE: / 'Attribute MATERIAL TYPE is : '. DATA return TYPE swotreturn. i_objtype = p_busobj.i_objtype TYPE swo_objtyp. i_element TYPE swo_verb. DATA object TYPE swo_objhnd. Lets execute the report and see the output: . ENDIF. * Instantiate the business object. ENDLOOP. LOOP AT lt_container INTO line. IF return-code = 0.

i_objkey TYPE swo_typeid. PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006'. DATA verb TYPE swo_verb. p_access TYPE c DEFAULT 'C'. . The Method DISPLAY will display the material passed in BO container. * Get an attribute of a business object. DATA return TYPE swotreturn. p_attr(32) TYPE c DEFAULT 'DISPLAY'. DATA object TYPE swo_objhnd. p_key(70) TYPE c DEFAULT 'ZSHUKSWE20' . i_element TYPE swo_verb. "To call method put 'C' DATA: i_objtype TYPE swo_objtyp. *&---------------------------------------------------------------* *& Report ZSWET_BO1 *& *&---------------------------------------------------------------* *& To call method with import parameters *& *&---------------------------------------------------------------* REPORT zswet_bo1.Now lets see how to call a method of BO.

e give it a key and create it. CALL FUNCTION 'SWO_INVOKE' EXPORTING access = p_access object = object verb = i_element IMPORTING return = return verb = verb TABLES container = lt_container. i_objtype = p_busobj. *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. The Output will be:- .DATA lt_container TYPE STANDARD TABLE OF swcont. * To call the Method of the BO. I. i_element = p_attr. *Instantiate the business object. i_objkey = p_key. CALL FUNCTION 'SWO_CREATE' EXPORTING objtype = i_objtype objkey = i_objkey IMPORTING object = object.

Remarks: When we checked the parameters of method ‘DISPLAY’ for business object ‘BUS1001006’. we will know the answer. we may only see two parameters: (1) materialEVG (Import parameter – optional) and (2) Return (Export parameter). Why is like this? … When we checked the BAPI called by this method. . In fact. From above codings. there is no need to pass any import parameter to the container. we may find that the Import parameter which is passed to the container is ‘MATERIAL’ instead of ‘MATERIALEVG’.(1) MATERIAL (mandatory) and (2) MATERIAL_EVG (optional). The method ‘DISPLAY’ calls BAPI ‘BAPI_MATERIAL_DISPLAY’ and the import parameters of it are listed as below :. Let’s see how the BAPI ‘BAPI_MATERIAL_DISPLAY’ is called in the method ‘DISPLAY’.

Lets see an example below on Howto Call the methods using the macros defined in include <CNTN01>.From below screenshot. *&----------------------------------------------------------------* *& Report ZSWET_BO1 *& *&----------------------------------------------------------------* *& Using macros defined in CNTN01 include. To call method of BO *& For reference check FM DNO_DB_APPENDIX_INSERT . we may find that the ‘Material’ import parameter is auto-populated by the the object key and not from the container.

"To call method put 'C' DATA: i_objtype TYPE swo_objtyp. i_element TYPE swo_verb. DATA: gv_mat TYPE swc_object. p_key(70) TYPE c DEFAULT 'ZSHUKSWE20'. "'MATERIAL' ->import parameter *For container of type table use swc_set_table. * Get an attribute of a business object. p_attr(32) TYPE c DEFAULT 'DISPLAY'. ***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. *Define container swc_container container.*&----------------------------------------------------------------* REPORT zswet_bo1. then CONTAINER will have the values *and we can read from container The Output will be:- . i_objkey = p_key. p_access TYPE c DEFAULT 'C'. *If there are any export parameter. INCLUDE <cntn01>. **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). *Create instance of Object type swc_create_object gv_mat i_objtype i_objkey. i_objtype = p_busobj. i_objkey TYPE swo_typeid. PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006'. i_element = p_attr.

p_key(70) TYPE c DEFAULT 'ZSHUKSWE20'. DATA: i_objtype TYPE swo_objtyp. PARAMETERS: p_busobj(10) TYPE c DEFAULT 'BUS1001006'. i_objkey TYPE swo_typeid. * Get an attribute of a business object. i_objkey = p_key. *Define container swc_container container. An explicit commit work is required after the FM call to trigger the event. INCLUDE <cntn01>.4To trigger an Event of BO We can use the FM ‘SWE_EVENT_CREATE’ to raise an event in any report program.1. *To clear a continer swc_clear_container container. other wise event will not be raised. i_objtype = p_busobj. *&---------------------------------------------------------------* *& Report ZSWET_BO1 *& *&---------------------------------------------------------------* REPORT zswet_bo1. *You can populate the Container if required *To generate event CALL FUNCTION 'SWE_EVENT_CREATE' .

" MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO " WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4 " RAISING OBJTYPE_NOT_FOUND. Event linkage can be done in SWE2 or SWETYPV transactions. IF sy-subrc <> 0. Just triggering an event does not makes any sense until and unless a proper event receiver id is linked to the event. Additional Notes . This event receiver can be a FM or can be any workflow.EXPORTING objtype = i_objtype objkey = i_objkey event = 'CREATED' TABLES event_container = container EXCEPTIONS objtype_not_found = 1 OTHERS = 2. ENDIF. COMMIT WORK.

.1. Understand the color legend of the methods for a business object.

What if the Business Object contains multiple keys? When creating the BO.scn.2. we will need to concatenate the keys together into an object key. Difference between “IMPLEMENTED” method and “RELEASED” method a. IMPLEMENTED – can only be called within SAP and usually used for testing purpose only b. http://wiki.sap. RELEASED – can be invoked within SAP and also by external application 4. .com/wiki/display/ABAP/A+step+by+step+guide+for+beginners+o n+user+defined+BAPI+creation 3.