Wednesday 21 October 2015

Creation of business objects, methods, events and event linkage

So it's a post after a long time. I have been thinking of writing a post on Business object since long, but thanks to the laziness that it took so long to finally have the courage to sit down for next atleast an hour :).

So we are with the business object. I am sure we all must have come across this term in our life in SAP. Most of the time we have heard of BUS0001 (BO for purchase order), BUS2032 (BO for sales order). These are all standard BO. First of all the T-code for display, change, creation of BOs is SWO1. So if you go in SWO1 and try seeing these standard BOs, you will get to see there are tree like structure for Interfaces, Key fields, Attributes, Methods, Events.



Let’s first understand the concept of business object. Apart from the existing BOs we can create our own custom BOs and also we can enhance the existing standard BO. So when is the time we have to create a custom BO? First of all we have to look upon the BO concept as some object which has one central entity (Key Field) and around that central entity we will create our functionality (Methods) and properties (Attribute). We create one BO in SWO1, define it’s key field , methods and event there. Now while programming for BOs, we have to create one instance of BO and while doing so we pass the key field. Means we are creating that instance of BO for that particular field. Now we can create further instances also with some other key fields. Also we can call the BO methods with the BO instance we have created. So Method will be called using business object instance, and because BO instance is for some key field, so method which we are calling is also for that particular key field. We can understand this concept of BO as classes. Statically we define our class, attributes, and methods. But while programming we actually instantiate the class and work with that instance. 




We can quickly make ourselves clear on this BO concept by using an example of student. Student is a central entity and around that entity we can create our functionality. So we can create a BO in SWO1 for student, where obviously the key field will be the roll no. Now what all functionality we can think of related to a student, make those many methods. Means we can have a method named ‘Add’, which will have the functionality to add the new student record in backend tables. Another method named ‘Attendance’ can simply calculate the overall attendance of the student, one method named ‘CPI_calculation’ can calculate the CPI for a given semester and so on and on. So here we have chosen Student as the central entity and we are designing a BO around that central entity. Now when we go into coding, we will be instantiating this BO and in the process we will be passing the key field (Roll no). Now we have a BO instance for a student whose roll no is say 101. Same way we can have another instance for another student. And now if those students are new to the system, we will call the method ADD 2 times with the help of 2 BO instances and these 2 student’s record will be updated in back end system.




Similarly we can create the events also in BO. Along with event creation we have to do event linkage also in SWE2. This event linkage tell what to do when this event gets triggered.

So below we will see the step by step process of BO, method and event creation and event linkage.

BO creation:
Go to SWO1. Give the name of object type and click on create. Fill the blank fields.  

Supertype will remain blank, as it comes into use when we are doing any enhancement in existing BO. That time we create new BO by keeping existing BO as supertype. This way new BO inherits all attributes, methods, events of existing BO.

Program name we have to give here. This program will be generated as soon as BO is created. Each time we add a method, this program gets changed. Below is the state of this program after we do okay in Create Object Type screen.



As you can see in the Changes Object Type screen, BO by default uses the interface IFSAP and methods ExistenceCheck and Display comes automatically attached to it. Because these 2 methods are in the interface IFSAP.

Key Field Creation:
Now we have to add the Key Fields to it. Place cursor on Key Fields and click on create.
 
Click on Yes.
Whatever table we are choosing, when we press enter it will show only the key fields of that table. So here only MATNR field is coming. Choose MATNR and click ok.




Save the object type. As soon as you save the object, you can check in the program, changes related to Key Field will be reflected there. Keep on checking the program every time we do the changes in object type. I’ll put the final screen shot of program at the end.

Method creation:
Place cursor on Methods and click on create.
 






Inside a method we will either be calling a function module, a report, a T-code. What this above message is asking us if we want to create method which will use function module inside. If we want to use FM inside method click on yes otherwise click on no.

If we click on no, it will lead us to this screen, where we have to tell out of available option which one we are going to use and have to give the name of the object. Like in this case we have to give the name of the report which will be submitted inside that method. That report must be an already existing report. So we have to create one sample report well in advance.

Now in result tab, we have to give what kind of result we are expecting in return from this method. If result is going to be a method, we have to select the check box of Multiline.



We will create another method which will use the function module template. So if we click Yes in Created Method screen.



Click on Next Step.


Click on Yes. If you click here No, method will be created, but it’s corresponding code will not be inserted in BO program. That you can do further by placing cursor on the method and click on Program.

Now we have not created any parameter which will be passed to these methods. We can do so by putting cursor on the method name and clicking on Parameters.





We can define whether it’s a import or export parameter, whether it’s a mandatory one and if it is supposed to be a table.





Event Creation:
As Usual put cursor on event and click on Create.



Now we will see the state of BO program how does it look now.



Now place the cursor in each method and event and go to the below mentioned path and do Modeled, implemented, release one by one


Again put the cursor on top on object type and go to below mentioned path to model, implement, and release it.



Now click on Generate (The red and white ball)

Event Linkage:
Event linkage is necessary to tell the system that what to do when this event gets triggered. So we will be telling it here that okay trigger this workflow (WS90000011) whenever this event gets triggered.

So for creation of this event linkage go to SWE2. Click on new entries and fill the entries.





Click on save and go back. You will find yours this entry in main screen among the sea of other ones.



So here this post has come to an end. I know it has been a long one. Thanks for bearing it with me. In next post we will have a look into how to use BO in coding, how to call its methods and events and how the workflow will come into picture when event gets triggered.

Till then Happy Learning :)