Showing posts with label BADI. Show all posts
Showing posts with label BADI. Show all posts

Sunday, 20 September 2015

BADI implementation

Here we go witht he step by step guide on BADI implementation. In the first 2 posts, we have seen about the creation of BADI and about the fallback class. Here is the quick link of earlier posts. Post 1 & Post 2 . In this last post of this BADI series, we'll see about BADI implementation.
First Approach:
Go to the enhancement spot and click on the create BADI implementation button.


Give the Enhancement Implementation name and it's description. What enhancement spot is to BADI, the same is Enhancement implementation to BADI implementation. Enhancement implementation contains the BADI implementation.



Because we have a fallback class in place, we will get the below information.


Click on empty class.



Click on method to implement it.


activate every object. Now execute the program which we created in last post, where we use this BADI. Earlier because there was no active BADI implementation, fallback class's method was executed. This time because an implementation does exist, method from implementation should be called.

The highlighted text is coming from BADI implementation.

Second approach:
Because we have created this BADI, so we are aware about the enhancement spot name and everything. Otherwise in daily business when technical developer doesn't know about which BADI needs to be implemented, we take help of method GET_INSTANCE of class CL_EXITHANDLER. This class comes into picture whenever a BADI comes in a code. GET_INSTANCE method gives us all the BADI names which are being used in a screen. So all we have to do is to put a breakpoint at this method and do the normal execution. Everytime control stops at this breakpoint means a BADI has been used and parameter EXIT_NAME of method GET_INSTANCE gives us the BADI name. These BADIs are the classic BADIs. In SE19 we have that option of New BADI and Classic BADI. So now we have the BADI name and we need to implement it.

Goto SE19. In create implementation block, choose the classic BADI option and give the BADI name.
  
 Click on Create implementation button





We can see the implementing class here and we have 2 methods here. We can go inside that methods to implement it. 


So this is how we implement the classic BADIs.

Happy Learning :)

Monday, 14 September 2015

Fallback class: A workaround of BADI implementation

In earlier post we discussed about why we need to create BADI and steps to do it. If you have missed that post Here You Go

As we have now created the BADI, we would be seeing how to use it in our code. I have created a simple ABAP program with one line to print.


Now I'll be calling that BADI in the code.


Now if we execute this program, it will go into dump.


The reason for this is that there is no active implementation exists yet for this BADI.

Note: Remember this dump occurs only if  'Multiple Use' option is unchecked in BADI definition. Means if you use in your code a BADI, for which multiple implementation CAN exists, but no active implementation yet exists, this dump will not occur.

So what is the remedy for this dump ? Well, we have not one but three :)

The very first is to catch the exception CX_BADI_NOT_IMPLEMENTED in your code. like below. Code will not go into dump instead will print the last line.



Second option is to implement the BADI, which we will cover in next post.

The third and the last is the magical Fallback Class :) . The dictionary meaning for fallback is 'an alternative plan that may be used in an emergency'. That's what exactly the fallback class is meant for.

Fallback class comes into picture when there is no active implementation exists for the BADI. In such case the code in fallback class's method executes. As soon as an implementation exists in system, the implementation call will be used automatically. This fallback class we give when we create the BADI definition.

We will be ticking the option for call Fallback class and a class name needs to be given there. Click on adjacent edit button.

Now if you execute the code, Both texts will be printed. One from the fallback class and one from the actual code.


Happy Learning :)

Friday, 4 September 2015

Creation of BADI

Yes, you heard it right..It's about creation of BADI not the implementation of an existing ones. :)

So why do we even need to create a BADI? Well, SAP comes as a product to us SAP folks. Product means we are not allowed to touch the SAP standard code. When required we can only enhance the standard code. This enhancement works alongside the standard code. BADI is one of the few enhancement techniques SAP gives to us.

So, what if we create say some tool in SAP and we have few customers also who use that tool. Sounds cooool :). So to customers, this tool is a product. They must not be able to touch the standard/basic code of the tool. Now if user wants to do some sort of enhancement in the tool (of course they would be, they have paid for it), we have to provide the BADIs to accomplish this. So that's oen of the few cases where we would be creating BADIs.

Enhancement spot: Enhancement spot works as a container for BADI definition. Single enhancement spot may have multiple BADI definition.

BADI definition: BADI definition is the place which contains the interface. That interface will have methods in it. When we say we are implementing that BADI, we actually create a class, use that interface in it and implement the methods. These implementation of the methods are the place where user would be writing the custom code.

To start with, we would be creating an enhancement spot. Go in appropriate package, right click on it and follow the below path.



Now create BADI definition.




 You can check or uncheck the Multiple Use option, depending upon your requirement. If you want to do more than one implementation of the BADI, leave it as checked.

Create interface for BADI definition and activate everything.






What????? We are done with the BADI creation. Happy Learning :)