Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to activate or deactivate a user-exit based a specific condition

Former Member
0 Kudos

hi all,

i want to activate or deactivate(make it trigger) a particular user-exit based in a condition.

can i do that. if yes please tell me how.

can we use COMMIT in user-exits or BADI's.

Thanks & Regards,

Saroja.

4 REPLIES 4

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

What you can do is in the user exit, do your condition check as the very first thing that you do. and issue an EXIT statement if you do not want the code within the user exit to be executed.

If sy-uname = 'RICHH'.
  exit.
endif.

Regards,

RIch Heilman

uwe_schieferstein
Active Contributor
0 Kudos

Hello Saroja

The solution provided by Rich should be used for testing purposes only in the the reverted sense:

IF ( syst-uname ne '<specific user>' ).
  RETURN.
ENDIF.

" Execute user-exit for specific user
...

However, for serious programming you should use a a better strategy. In principle, user-exits are either ON or OFF and, if they are ON, they are ON for <b>all </b>user which is usually not intended.

The following example shows a (possible) strategy how to execute user-exits based on specific conditions.

The SAP extension CATS0001 contains the component EXIT_SAPLCATS_001 with the following interface:

FUNCTION EXIT_SAPLCATS_001.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
*"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
*"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
*"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
*"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
*"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
*"  TABLES
*"      SAP_ICATSW STRUCTURE  CATSW
*"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
*"----------------------------------------------------------------------


  INCLUDE ZXCATU01.


ENDFUNCTION.

The include ZXCATU01 contains only the following coding:

  CALL FUNCTION 'Z_EXIT_SAPLCATS_001'
    EXPORTING
      sap_tcats            = sap_tcats
      sap_pernr            = sap_pernr
      sap_dateleft         = sap_dateleft
      sap_dateright        = sap_dateright
      SAP_DATEFROM         = SAP_DATEFROM
      SAP_DATETO           = SAP_DATETO
    tables
      sap_icatsw           = sap_icatsw 
      SAP_ICATSW_FIX       = SAP_ICATSW_FIX.

This function module is just a copy of the exit function module in the customer namespace.

Let us assume that your condition at which the user-exit should be executed is that the employee (SAP_PERNR) belongs to a specific controlling area. Thus, we make another copy of the original exit function module and call this fm within the "general" customer-specific exit function module:

FUNCTION z_exit_saplcats_001.
*"--------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
*"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
*"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
*"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
*"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
*"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
*"  TABLES
*"      SAP_ICATSW STRUCTURE  CATSW
*"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
*"--------------------------------------------------------------------


" User-Exit specific for employees (SAP_PERNR)
" belonging to controlling area 1000
  CALL FUNCTION 'Z_EXIT_SAPLCATS_001_1000'
    EXPORTING
      sap_tcats      = sap_tcats
      sap_pernr      = sap_pernr
      sap_dateleft   = sap_dateleft
      sap_dateright  = sap_dateright
      sap_datefrom   = sap_datefrom
      sap_dateto     = sap_dateto
    TABLES
      sap_icatsw     = sap_icatsw
      sap_icatsw_fix = sap_icatsw_fix.



" User-Exit specific for employees (SAP_PERNR)
" belonging to controlling area 2000
  CALL FUNCTION 'Z_EXIT_SAPLCATS_001_2000'
    EXPORTING
      sap_tcats      = sap_tcats
      sap_pernr      = sap_pernr
      sap_dateleft   = sap_dateleft
      sap_dateright  = sap_dateright
      sap_datefrom   = sap_datefrom
      sap_dateto     = sap_dateto
    TABLES
      sap_icatsw     = sap_icatsw
      sap_icatsw_fix = sap_icatsw_fix.

ENDFUNCTION.

Finally, within the specific exit function module we define the condition when the exit should be executed:

FUNCTION z_exit_saplcats_001_1000.
*"--------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(SAP_TCATS) LIKE  TCATS STRUCTURE  TCATS
*"     VALUE(SAP_PERNR) LIKE  CATSFIELDS-PERNR
*"     VALUE(SAP_DATELEFT) LIKE  CATSFIELDS-DATELEFT
*"     VALUE(SAP_DATERIGHT) LIKE  CATSFIELDS-DATERIGHT
*"     VALUE(SAP_DATEFROM) LIKE  CATSFIELDS-DATEFROM OPTIONAL
*"     VALUE(SAP_DATETO) LIKE  CATSFIELDS-DATETO OPTIONAL
*"  TABLES
*"      SAP_ICATSW STRUCTURE  CATSW
*"      SAP_ICATSW_FIX STRUCTURE  CATSW OPTIONAL
*"--------------------------------------------------------------------


  IF ( <user BELONGS to CONTROLLING area 1000> ).
    "   execute user-exit
  ELSE.
    RETURN.
  ENDIF.



ENDFUNCTION.

The alternative would be to place the entire coding including the conditions in the include ZXCATU01. However, in this case you can test the user exit <b>only in the context of the transaction</b> in which the user-exit is passed.

Using the strategy I have devised you are able to test the user-exit in general and the specific user-exits <b>independent </b>of the transaction. For example, if you are already working on 6.40 or higher then you could use ABAP Unit Testing for this purpose.

The same logic can be applied for BAdI where we can have only a single active implementation.

Finally, I hope to convince that it makes sense to spend some time into a reasonable strategy for implementing user-exits.

Regards

Uwe

uwe_schieferstein
Active Contributor
0 Kudos

Hello Saroja

I have forgotten to answer your second question. Since user-exits are executed in the middle of a transaction we should not use COMMIT WORK. However, you could try and put your logic within a function module and call this function module "IN UPDATE TASK".

Regards

Uwe

0 Kudos

Actually, my example condition I guess was a little misleading. My intension was not to give the impression that you are only allowing the user exit for certain people, just a condition. I guess I should have said......

If sy-subrc <> 0.
  Exit.
Endif.

or...

If a <> b.
   Exit.
Endif.

Regards,

Rich Heilman