Useful SAP Business Object Type Programming Macros - SAP Community
Useful SAP Business Object Type Programming Macros - SAP Community
GopalNair
Participant
05-29-2013 7:43 AM
10 Kudos
16,232 Views
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.
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 1/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
REPORT zgn_object_create.
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.
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.
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 2/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
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:
For example, to get a list of all movies playing in a theatre, the following can be used:
Where,
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 3/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
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 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:
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.
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
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 4/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
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:
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:
Tags:
Add tags
Comment
Comments
05-30-2013
keohanster Active Contributor
7:34 PM
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 5/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
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.
Regards,
Sue
05-30-2013
rambabu_k3 Active Participant
7:48 PM
To add one point... Please check INCLUDE program <CNTN01>. which contains all
these macros.
Thanks
Rambabu
05-30-2013
GopalNair Participant
8:41 PM
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
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 6/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
05-30-2013
anjan_paul Active Contributor
10:13 PM
Yes Gopal. It will very helpful to see macro in one place with uses.
07-29-2014
Former Member
4:08 PM
Please note that this macro can be used only within an object method. Syntax is as follows:
See,
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 7/8
8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community
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.
03-15-2016
Former Member
7:23 PM
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/technology-blog-members/article-id/27534 8/8