Skip to Content
Author's profile photo Gopal Nair

Useful SAP Business Object Type programming macros

Figuring out the Key to instantiate an Object

Go to SWO1 transaction and display the object type you want to instantiate. Now, at the top, click on the “Program” button, as shown in the figure.

/wp-content/uploads/2013/05/find_key_223375.png

You will notice that there is a section starting with “BEGIN OF KEY” and ending with “END OF KEY”. This is your key declaration. You will want to create a type of the same structure in your ABAP application, before you can instantiate the object type in your application.

Instantiating a Business Object

To instantiate a business object, first create a Type/structure using aforementioned technique. For the above object, the type declaration will be as shown:

REPORT  zgn_object_create.
 
DATA: BEGIN OF objkey,
    airline LIKE sflight-carrid,
    connectionnumber LIKE sflight-connid,
    flightdate LIKE sflight-fldate,
END OF objkey.

One More Step Before Using the Business Object Programming Macros

All the macros we are going to discuss here is defined in the include program “CNTN01”. Hence, the first step before using any Business Object Programming macros would be to include CNTN01 object in the application like so:

REPORT  zgn_object_create.
INCLUDE <cntn01>.
DATA: BEGIN OF objkey,
    airline LIKE sflight-carrid,
    connectionnumber LIKE sflight-connid,
    flightdate LIKE sflight-fldate,
END OF objkey.

SWC_CREATE_OBJECT – Instantiate Object Reference.

The SWC_CREATE_OBJECT macro is used to instantiate a business object. The syntax is as follows:

SWC_CREATE_OBJECT <Object> <ObjectType> <ObjectKey>

 

All object references, irrespective of the actual object type, have the data type definition SWC_OBJECT.

For example, for instantiating a customer object, the following could be the code.

Data Customer TYPE SWC_OBJECT.
SWC_CREATE_OBJECT Customer 'KNA1' customerID.

SWC_GET_PROPERTY – Macro to get a single valued attribute

SWC_GET_PROPERTY macro can be used to access single valued attributes from object instance. Syntax is as follows:

SWC_GET_PROPERTY <Object> <Attribute> <AttributeValue>

When attempting to access attributes of the same object (say, when developing methods, you want to access the attributes of the object), we can use “self” as the object name. For example, to access an attribute “Name”, the following can be used:

SWC_GET_PROPERTY self 'Name' namevalue.

SWC_GET_TABLE_PROPERTY – Macro to get multiple value attribute

SWC_GET_TABLE_PROPERTY macro can be used to access multiple valued attributes (Multi Line attributes). Syntax is as follows:

SWC_GET_TABLE_PROPERTY <Object> <Attribute> <Value>

For example, to get a list of all movies playing in a theatre, the following can be used:

SWC_GET_TABLE_PROPERTY Theatre ‘MovieList’ Itab_movies.

Where,

  • Theatre is the Object Instance
  • MovieList is the attribute name (Multi Line Attribute)
  • Itab_Movies is an internal table to hold the values of the multi line attributes.

SWC_SET_ELEMENT – Macro to set a single valued attribute

SWC_SET_ELEMENT macro can be used to set single valued attributes in an object instance. Please note that this macro can be used only within an object method. Syntax is as follows:

 

SWC_SET_ELEMENT CONTAINER <AttributeID> <Value>

Because there is no object reference in the syntax, it is obvious that the use of this macro is pretty much restricted to within a method in the object instance. This in one way is a good thing, as this makes it impossible to modify the attribute values of a business object directly. If we have to modify the attribute value of a business object, we have to use the object methods, and use the set element macro inside that method.

SWC_SET_TABLE – Macro to set multi line attribute.

SWC_SET_TABLE macro can be used to set multi line attributes in an object instance. Please note that this macro can be used only within an object method. Syntax is as follows:

 

SWC_SET_TABLE CONTAINER <AttributeID> <Value>

 

As mentioned before, since this macro does not have an object instance reference, this macro can be used only inside an object method, adding a layer of control on changing attribute values of a business object instance.

EXIT_RETURN – Return exception information from a method.

When developing the business object methods, it is important that any problems that the method encounter be properly communicated back to the caller. This may not be a very strict requirement when making the method calls from ABAP programs. But, this is a big issue when making the method calls from work flows (Tasks). Raising an exception, and giving information back to the framework that something went wrong in method execution is the only way by which the workflow can take evasive/alternate paths to get the work done (Error Handlers). Since there is no saying where all the business object will be used (may be today, it would be used just in your program, but in future, someone may decide to use your object in a workflow), its always a good practice to raise appropriate errors from methods if something goes wrong. The syntax is as follows:

EXIT_RETURN <Exception> <Var1> <Var2> <Var3> <Var4>.

 

The EXIT_RETURN macro specifies the exception number and the parameters for the message. Messages can have a maximum of 4 parameters and all the parameters (Var1 tro var 4) are mandatory. So, what if I have only 1 parameter to send? Well, we just use “SPACE” in place of other parameters like so:

 

EXIT_RETURN <ExceptionID> <Var1> SPACE SPACE SPACE.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Susan Keohan
      Susan Keohan

      Well done, Gopar!

      I know many people would wish these macros would go away - and yes, it does take a while to become comfortable with them.  This document should help many workflow developers achieve that level of comfort.

      Where was this when *I* was starting out!

      Regards,
      Sue

      Author's profile photo Gopal Nair
      Gopal Nair
      Blog Post Author

      Thanks, Sue! You are right... it takes some practice getting used to. But, once comfortable with these macros, it can be used in regular ABAP programs, and not constrained to Workflow development.

      Best Regards,

      Gopal Nair

      Author's profile photo Rambabu Kunuku
      Rambabu Kunuku

      To add one point... Please check INCLUDE program <CNTN01>. which contains all these macros.

      Thanks

      Rambabu

      Author's profile photo Anjan Paul
      Anjan Paul

      Yes Gopal. It will very helpful to see macro in one place with uses.

      Author's profile photo Former Member
      Former Member

      Please note that this macro can be used only within an object method. Syntax is as follows:

      SWC_SET_ELEMENT CONTAINER <AttributeID> <Value>

      Well, i doubt that  after reading documentation.Please correct me if i am wrong.

      See,

      2.PNG

      So it means if i have to trigger events from abap prog, i'll be needing event container.

      So these macros can also be used outside BOR Methods, if we need to pass an event container to FM ->SWE_EVENT_CREATE or SAP_WAPI_CREATE_EVENT.

      So i am wrote these macro's BOR-> KNA1 to create a event container.

      1.PNG

      please correct me if am wrong somehow.

      Author's profile photo Former Member
      Former Member

      Thx for this, it's very helpful!