working-with-business-objects-in-sap

Document Sample
working-with-business-objects-in-sap Powered By Docstoc
					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).
1.   1     Working 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.

2.   1.1    To create instance of a BO

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>
i_objkey = <BO key>
CALL FUNCTION 'SWO_CREATE'
  EXPORTING
    objtype = i_objtype
    objkey = i_objkey
  IMPORTING
    object = object.

The variable „object‟ will hold runtime instance of the object type.

The other way is to use the macros defined in include <cntn01>
INCLUDE <cntn01>.
DATA:
    i_objtype TYPE swo_objtyp,
    i_objkey TYPE swo_typeid,
    object TYPE swc_object.
*Create instance of Object type
swc_create_object object i_objtype i_objkey.
 Internally the macro calls the FM SWO_CREATE. So you can either go for direct FM call or the macro
call.

3.   1.2    Container

Basically the term Container is used with reference to Business Objects and Workflows. The Container
actually holds import and export parameters associated with any method of Business Object at runtime.
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.
Business Object container is technically of type SWCONT structure.
Here „ELEMENT‟ will be name of the variable/internal table which container holds and „VALUE‟ will
have corresponding value. For multi line variable or internal tables, the container will hold multiple
values with same element name.

4.   1.2.1   Lets 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.
Key field will be Purchase Order number (EKKO_EBELN) for BO.
Method READ will take Purchase Order number as input and will give all the Purchase Line items in it in
an internal table.
Step 1 : Create BO ZSWE1 for test purpose. You can give it any name.




Step 2 : Now create a method READ
Save the method. Now we will create the parameters of the method.

Import Parameters PurchaseDocument            EKKO-EBELN

Export Parameter          ITEM          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 )
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.

5.   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.

6.   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:




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:-




7.   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.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:30
posted:8/24/2011
language:English
pages:13