November 6, 2015

Find User-Exits and BAdIs in ABAP. Part 4

In the last post I showed a very simple but practical example about implementing a BAdI. This time I'll show how to implement a classical user-exit. As I wrote before, a BAdI is also a user-exit but in an object oriented version .

Also it's a matter of investigation and you must study all the possibilities when looking for a solution to your business requirement. Some stuff can't be definitely be done however there's at least a standard way to implement it.

For example, once I had this requirement where I needed that a replenishment transaction could divide all the generated purchase requests by the purchasing group. By standard this is not possible so it was necessary to create a program. Being SAP a complete ERP with the best practices in the business it's better to follow its instructions rather than re-inventing the wheel.

As we did in the previous post we must look for the user-exits in the transaction we are trying to enhance. When we have it we must use the transaction 'CMOD'. This transaction works creating projects where we group all the user-exits.

Before we use 'CMOD' we can check transaction 'SMOD', which is used to look for details of the user-exit like parameters and documentation if available.This same information can be watched in transaction 'CMOD', however, you must create the project first.

In this example we are going to implement a simple user-exit for transaction 'VKP1' (Change price calculation). The name of the user-exit is 'LWVK1003', which you can find with a program I posted before.

Let's implement it:

1. Execute transaction 'CMOD'.

2. Name the project 'ZMYENH'.

3. Click on button 'Create'.

4. Type a brief description of the project.

5. Click the button 'Enhancement assignments'. At this stage you'll be requested to create a transport request before saving.

6. Type in the textbox 'Enhancement' the user-exit name 'LWVK1003'.

7. In the same screen click the button 'Components'.

8. A screen with the user-exits is going to be shown. There you will see your enhancement. Double click on it.

9. Now a function module with the name 'EXIT_SAPLWVK1_003' is shown in the screen with the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function exit_saplwvk1_003 .
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(PI_KALP_UPDKZ) LIKE  KALP-UPDKZ
*"     VALUE(PI_I_KALP) LIKE  KALP STRUCTURE  KALP
*"     VALUE(PI_I_VORGA) LIKE  PISVR-VORGA OPTIONAL
*"     REFERENCE(PI_I_GLOB) TYPE  GLOB OPTIONAL
*"  EXPORTING
*"     VALUE(PE_I_ERRO) LIKE  ERRO_TEMP STRUCTURE  ERRO_TEMP
*"----------------------------------------------------------------------


include zxvkpu04 .


endfunction.

10. Double click on the word 'zxvkpu04'.

11. Copy and paste the following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
  case pi_kalp_updkz.

*-- Insert of a pricing position --------------------------------------*
    when 'I'.

*-- Update of a pricing position --------------------------------------*
    when 'U'.
*---- Sales price is less than net-net-purchase price -----------------*
      if pi_i_kalp-vkpne lt pi_i_kalp-ekpnn.
        pe_i_erro-msgid = 'WV'.                      "Message class WV
*       PE_I_ERRO-MSGTY = 'E'.                       "Error
        pe_i_erro-msgty = 'W'.                       "Warning
        pe_i_erro-msgno = '463'.                     "Message no 463
        exit.
      endif.

  endcase.

12. Activate the code.

13. Return to function module with 'F3'.

14. Return to the project.

15. Activate the project and exit of transaction 'CMOD'.

16. Execute transaction 'VKP1' and test it with any material. 


When I execute transaction 'VKP1' and the sales price is less than net purchase price the system responds with a warning.



You can debug placing a break-point in your code and check the execution.




Finally I would like to add that in a project you can add as many user-exits as you may want. Just try to group them according to you business module and areas.

See you in the next.

Hope it helps.
Please Share it! :)

No comments :

Post a Comment