0% found this document useful (0 votes)
41 views8 pages

Useful SAP Business Object Type Programming Macros - SAP Community

The document provides an overview of useful programming macros for SAP Business Object Types, including how to instantiate objects and access their properties using specific macros. Key macros discussed include SWC_CREATE_OBJECT, SWC_GET_PROPERTY, and SWC_SET_ELEMENT, along with their syntax and usage examples. It emphasizes the importance of including the CNTN01 program to utilize these macros effectively in ABAP applications.

Uploaded by

Ganesh Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Useful SAP Business Object Type Programming Macros - SAP Community

The document provides an overview of useful programming macros for SAP Business Object Types, including how to instantiate objects and access their properties using specific macros. Key macros discussed include SWC_CREATE_OBJECT, SWC_GET_PROPERTY, and SWC_SET_ELEMENT, along with their syntax and usage examples. It emphasizes the importance of including the CNTN01 program to utilize these macros effectively in ABAP applications.

Uploaded by

Ganesh Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

8/6/24, 11:06 PM Useful SAP Business Object Type programming macros - SAP Community

SAP Community > Products and Technology > Technology


> Technology Blogs by Members > Useful SAP Business Object Type programming macros

Useful SAP Business Object Type programming macros

GopalNair
Participant
‎05-29-2013 7:43 AM

 10 Kudos

16,232 Views

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:

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.

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.

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

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

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

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

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.

Tags:

abap basics of workflow beginner Business Workflow new to workflow

sap business workflow sap workflow starting with workflow Workflow

workflow basics workflow starter

Add tags

Comment

Comments

‎05-30-2013
keohanster Active Contributor
7:34 PM

Well done, Gopar!

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.

Where was this when *I* was starting out!

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:

SWC_SET_ELEMENT CONTAINER <AttributeID> <Value>

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

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.

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

please correct me if am wrong somehow.

‎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

You might also like