September 4, 2015

Find User-Exits and BAdIs in ABAP. Part 3

Hi, in the previous posts I wrote about how to customize SAP using user-exits. If you haven't read them I encourage you to go to part 1 and part 2 before you continue.

A user-exit can be a new field in a screen or a new functionality. These user-exits are often provided by SAP however is possible to make non-standard changes through explicit enhancements and coding directly in the source code.

In this post we are going to explain the implementation of a BAdI. BAdI stands for "Business Add-In" and is basically a class that is instantiated with custom code of our own. These classes are pre-defined and are located in strategic places in order to change data and sometimes to add a screen with custom data.

The search of BAdIs (and all user-exists in general) is esential in this stage. It's also helpful to read the documentation in SAP and in scn.sap.com to understand the impact and the possible consequences of the changes you want to perform.

The example I want to show is to implement the BAdI 'BADI_MATERIAL_CHECK'.

So, let's code ABAP:

1. Execute transaction 'SE18'.

2. Type in BAdI name 'BADI_MATERIAL_CHECK'.

3. Let's make a pause to explain what we are watching:

The first screen shows two tabs; the first one indicates the package it belongs, the language and who and when was made the last change in the BAdI.

The other section is very interesting because it shows you how this class is going to be used. There's an option 'Wtihin SAP' which is internal and can only be modified or implemented by SAP. When this option is not checked then you can proceed to create your own implementation. I haven't done it yet however you could make your own BAdIs and implement them in your programs.

The other important option is 'Multiple use' which means that you can create one or more implementations from this BAdI. If this option is not checked it means that one and only one implementation can be made for this BAdI.


The other tab 'Interface' shows all the methods that can be implemented. In this stage you must check which fits your requirement and also its parameteres.


For our example double click on method 'CHECK_DATA' and let's review the parameters. First of all, let's say that there are four types of parameters:
  1. Importing
  2. Exporting
  3. Changing
  4. Returning


By default the IMPORTING parameters come with data but can't be changed. The EXPORTING parameters are filled with data to be used outside the BAdI to follow the flow of the program and the CHANGING parameters are the same as EXPORTING but these are reference of data that comes from the program flow. The RETURN parameters work very similar to the EXPORTING parameters.

4. Goto menu Implementation / Create


5. Type an implementation name. This name must be inititiated with 'Z' or 'Y'.


6. Type a description for the implementation.


7. Go to 'Interface' tab and double click method 'CHANGE_DATA'.


8. There are two CHANGING parameters that are permitted to be modified, so we can use parameter 'STEXT' to have a value. Copy and paste the following code.

method IF_EX_BADI_MATERIAL_CHECK~CHECK_DATA.
  data wa_stext like line of stext.
  wa_stext-spras = 'S'.
  wa_stext-maktx = 'My text'.
  append wa_stext to stext.
endmethod.

9. Activate the changes and create a transport order if it is asked.

10. Finally, after the changes are made to the method, return and activate the BAdI. You must perform this action once, normally the first time. If you want to deactivate it click on the button at the right.

10. Set a breakpoint in the code inside the BAdI and create a material with transaction MM01. After you press the save button the debugger will pop up in the breakpoint you set. This was made intentionally to show how the BAdI will function.


Press F8 key to continue.

11. After the material is created go to transaction MM03 to visualize the material you recently created and then click on the 'Additional data' button.


13. Watch the results.



Of course, this is a simple example but other BAdIs involve to create dynpros and many other times there are no EXPORTING or RETURNING parameters and you must find other BAdIs or study a little more in order to achieve the task. I recommend to study the documentation and/or go to scn.sap.com forums. There are people who already done your solution.

WARNING: PLEASE, DO NOT PERFORM A COMMIT WORK, ROLLBACK OR RAISE ANY MESSAGE inside a BAdI. This action could lead to erroneous and inconsistent data in SAP because the BAdI instance is inside a LUW and committing/rolling back will perform changes in database without ending the process.

In the next article we'll implement a user-exit with transaction ' CMOD'.

See you in the next.

Hope it helps.