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