Sei sulla pagina 1di 7

SAPtips4On ABAP/J2EE

February / March 2007 Volume V Issue 1

SAP User Exits And The People Who Love Them

Page 

By Jeff Goldstein, IPM Inc.


Editors Note: New contributor Jeff Goldstein starts out with an excellent discussion of the process of deciding when/where/whether a developer should write custom code for the SAP applications. He then moves on to help us understand a technique for passing variables in user exits that may not be as well known or appreciated, as it should be. This article could prove extremely valuable in not only helping keep your environment clean, but also in adding flexibility and power to your users experience. includes R/3, SCM, BW, and APO. The theory behind this article applies to all SAP ABAP based products; however the concrete examples and screen shots are derived from an R/3 4.7 system. can be built and maintained using the inherent framework of the packaged system is clearly more elegant and more maintainable. Thus we have the responsibility to ensure that this avenue is fully explored before considering other options. This may prove difficult since often the developers are not even brought into the discussion until such time as a system configurator has determined that configuration (or customizing, as some prefer to call it) is not possible. Despite this we can gain respect from our more functionally oriented colleagues when we do our part to see to what extent we can use configuration. Try out my common sense approach: How common is what I am doing? That is, try to discern whether your business process is quite unique or if this is likely something that has been faced by many others before you. If it seems to be clearly something that weaves a common thread across all or most businesses, then you should really search hard for a configuration-based solution, as the tools for it probably do exist. If it falls more on the unique end of the spectrum, you should still perform due diligence and check the IMG, the Internet, SAPs help resource (http://help.sap.com), your network of SAP savvy friends, and of course the SAPtips Document Library. Often a solution based in configuration will have some coding components, such as custom pricing requirements or dynamic actions. Once weve eliminated the possibility that the system has a pre-built, configuration-based solution, we can move on to the design and development of our own solution. Most often there will be many options for building a solution.
SAPtips 2007 Klee Associates, Inc.

The ugly truth of the matter is that we should discourage coding solutions whenever possible.

Introduction

SAPtips Journal

Before we get into a long discussion about user exits, we should consider the proper role that a user exit should play in SAP development. But before we can do that, we need to understand what exactly it is an SAP Developer should be doing. I choose the phrase SAP Developer rather than ABAPer or SAP Coder or Techno Nerd Person for a distinct reason. That being that we who are entrusted with building enterprise solutions using the complex SAP development platform really cannot limit ourselves to simply being coders. Rather we need to know the ins and outs of our SAP system as well as all the tools available to us within that system. We need to further understand (in full detail) the business process we are touching, and select the best tools and practices in order to build an elegant and maintainable solution. Thus, one who understands all these facets is more correctly referred to as an SAP Developer. The name SAP used to be synonymous with just SAPs flagship product, R/3. The name SAP now encompasses a multitude of products. Of these, my experience

Now let me dismount my high horse so that I may continue in a less preachy fashion. As we have a responsibility incumbent upon ourselves to build elegant solutions, we should now look at applying user exits as one such instrument to consider for this purpose. First let us consider if coding solutions should be used at all. The ugly truth of the matter is this: we should discourage coding solutions whenever possible. Since we all enjoy a livelihood based in large part on coding SAP solutions, this may sound counter intuitive. But in our overarching role as stewards of an enterprise system, it makes perfect sense. A solution that

SAPtips.com

SAPtips4On ABAP/J2EE
February / March 2007 Volume V Issue 1
Page 2

User Exits

To make SAP-based solutions easier, SAP has provided user exits. These user exits come in a variety of forms: BADIs, function modules, dynamic actions, SMOD/ CMOD, embedded forms such as those found in module pool SAPMV45A (which technically are repairs, but act as user exits), just to name a few. We should definitely consider the application of these user exits before considering the option of creating a custom process, custom screens, and custom tables. Fully utilizing the user exits before considering custom objects will help keep your enterprise system clean and stable. The questions of whether an appropriate user exit exists, and how one can find the appropriate user exit if it does exist, can be daunting. There are a couple of techniques that can bring harmony to this rather chaotic process. You can use SE81 to find all the user exits for a specific functional area. For example, if you wanted to Figure 1: List of Functional Areas with MM Highlighted find all the user exits within Materials Management you can place the cursor on MM You can double-click on Definiand then from the Edit menu choose tions, inside the BADI subfolder, or Select Subtree. At this point you Enhancements, within the Cushave highlighted the MM portion of tomer exits folder, and execute the the structure; if you were to expand corresponding search. Here you will the tree you would see that all of MM see a list of the user exit names along has been marked, as shown in figure with their short descriptions (Figure 1. 3). Following these steps is one easy way to determine if there is a user exit In figure 2 youll see that if you that can meet your needs. This methwere to click on the Information sys- od works well. However, my method tems push-button you would arrive of choice is still to debug through the at a new screen. In this screen you process and see what user exits I hit can expand the Enhancements fold- along the way. I know its slower, but er and see subfolders for Business I enjoy getting a feel for the transacAdd-ins and Customer exits. tion and whats going on at the point my user exit is called.

SAPtips Journal

Having a user exit in the appropriate spot is critical for customizing a process through coding. The other concern is having access to the proper variables within a user exit. Another way to say this is: having a user exit that doesnt allow for changing the parameters you need doesnt really help you much. This is what I used to think.

Variables

But here is the real substance of this article: There is a technique that allows you to greatly expand the scope of variables you can use inside your user exits. The significance of this
SAPtips 2007 Klee Associates, Inc.

SAPtips.com

SAPtips4On ABAP/J2EE
February / March 2007 Volume V Issue 1
Page 

should not be under-appreciated. Being able to have more variables to read and to change greatly enhances the power of user exits. When a colleague of mine showed me this technique about a year ago I was amazed that this powerful tool had escaped me for so long. I instantly thought back to all the shoddy code I produced along my way that could have been avoided if only I had known this simple technique. I also searched the Web to see if this technique had been documented, and could not find it. Although Im sure it must be documented out there somewhere, Figure 2: The Screen You See After Selecting the Information systems Push-Button I am going to do my part to increase the visibility of this simple yet profound coding technique. We should keep one thing in mind before delving into the specifics of increasing the available variables within SAP user exits. You will now have the power to change variables that will greatly affect the system. You will be able to change structures just before they are written to the database. The following technique is a powerful tool, and as with any tool it has the potential to be used properly or abused so please be very careful when changing variable values. This technique allows you to pull in variables from the call stack. When in the debugger, if you hit the Calls button you will see the list of programs leading up to the point where you are now in the code. Using the technique described here, you can pull variables that are found with- Figure 3: The List of MM Related Enhancements

SAPtips Journal

SAPtips.com

SAPtips 2007 Klee Associates, Inc.

SAPtips4On ABAP/J2EE
February / March 2007 Volume V Issue 1
Page 

in the scope of the call stack programs preceding the user exit. For example, if you are inside a pricing requirement and want to pull in information from SAPMV45A, the main module pool behind the sales order transactions, then you could use this technique to do so. Lets see how this might look for a form called from a custom pricing requirement, say 999. The program would be RV61A999 and the form might look like Figure 5. So here we have a form that would be called within a pricing procedure and we are able to pull in the xvbap table. It is important to make sure our assignment worked Figure 4: The Debugger Call Stack properly. In this case the pricing procedure can be called from an invoice pricing as well FORM hello_world_of_expanded_variables. Called from FORM KOBED_999 as sales order pricing. When called from the invoice, the stack will not DATA: contain SAPMV45A and the assignl_prog(50) TYPE C VALUE (SAPMV45A)XVBAP[], ment would fail. Making an assumption that it worked and then uing the lt_xvbap TYPE STANDARD TABLE OF vbapvb, lt_xvbap[] = <xvbap> command will ls_xvbap LIKE LINE OF lt_xvbap. cause a short dump.
FIELD-SYMBOLS: <xvbap>.

SAPtips Journal

It is important to make sure our assignment worked properly, if not, the command will cause a short dump.

* Get xvbap table from way back in call stack ASSIGN (l_prog) TO <xvbap>. IF sy-subrc NE 0. EXIT. ELSE. lt_xvbap[] = <xvbap>. * Now we xvbap table stored in a local variable and can read from it as needed ENDIF. ENDFORM.

Figure 5: Calling the Form

SAPtips.com

SAPtips 2007 Klee Associates, Inc.

SAPtips4On ABAP/J2EE
February / March 2007 Volume V Issue 1
Page 

This is just one very small example of pulling in a variable to a user exit that otherwise would be out of reach. This example is not practical, as I dont do anything with the data once it is retrieved; but it shows you the potential and power of this concept. As user exits are often severely limited by the variables that you can pass in or out of them, I think it is self evident that the technique demonstrated here allows one to vastly increase the potential variables that can be used, and therefore the greater flexibility it lends to user exits. Although the code snippet I just showed you does not illustrate it, once brought into the code, these variables can be read and changed as well. Adding the lines: CLEAR lt_xvbap. <xvbap> = lt_xvbap. Just before the ENDFORM statement would be ill advised, but would demonstrate the power of the technique. At a recent client, I needed to filter out CATS (timesheet) reporting based on cost center values. I took advantage of the CATS Data Selected method for the CATS Reporting BADI. In this example the client wanted to make sure the personnel number was valid for the cost center selected, at the time the user entered a cost center on the selection screen. Figure 6 illustrates the coding to accomplish this.

METHOD if_ex_cats_reporting~cats_data_selected . * Table CH_CATS_DATA is the table to change DATA: lt_cats_data TYPE catsdb_ext_itab, lt_pnpkostl TYPE STANDARD TABLE OF kostl_rang, l_prog(40) TYPE c VALUE (RCATS_DISPLAY_ACTIVITIES)PNPKOSTL[], ls_cats_data LIKE LINE OF lt_cats_data, ls_pernr TYPE pernr-pernr, lt_0001 TYPE STANDARD TABLE OF pa0001, lt_date_range TYPE RANGE OF sy-datum, ls_date_range LIKE LINE OF lt_date_range, ls_0001 LIKE LINE OF lt_0001. FIELD-SYMBOLS: <kostl> TYPE table. lt_cats_data[] = ch_cats_data[]. ASSIGN (l_prog) TO <kostl>. IF NOT sy-subrc IS INITIAL. EXIT. ELSE. lt_pnpkostl[] = <kostl>. ENDIF. CHECK NOT lt_pnpkostl[] IS INITIAL. SORT lt_cats_data BY pernr. *--- If user entered cost center only include pernr records when pernr *--- was in that cost center LOOP AT lt_cats_data[] INTO ls_cats_data. *-- For each pernr get dates for cost center(s). IF ls_cats_data-pernr <> ls_pernr. CLEAR: lt_date_range[], ls_date_range. ls_pernr = ls_cats_data-pernr. SELECT * INTO TABLE lt_0001 FROM pa0001 WHERE pernr = ls_pernr AND kostl IN lt_pnpkostl . CHECK sy-subrc IS INITIAL. MOVE: I TO ls_date_range-sign, BT TO ls_date_range-option. LOOP AT lt_0001 INTO ls_0001. MOVE: ls_0001-begda TO ls_date_range-low, ls_0001-endda TO ls_date_range-high. APPEND ls_date_range TO lt_date_range. ENDLOOP. ENDIF. IF NOT ls_cats_data-workdate IN lt_date_range DELETE lt_cats_data. ENDIF. ENDLOOP. ch_cats_data[] = lt_cats_data[].

SAPtips Journal

Figure 6: Cost Center Validation

ENDMETHOD.

SAPtips.com

SAPtips 2007 Klee Associates, Inc.

SAPtips4On ABAP/J2EE
February / March 2007 Volume V Issue 1
Page 

Conclusion

In summary, user exits are a very important part of letting us modify an SAP system. Further, knowing when and where to use them is crucial. Once you have identified a user exit thats applicable to your needs, you can use this technique to greatly expand its reach, by reading and changing variables from programs that are nested several layers in the system. Remember that this is a powerful tool, and you must be very conscientious in its use. Jeff Goldstein, Director of Consulting IPM-Inc. Jeff has over 11 years of SAP consulting experience working with large Fortune 500 companies and some mid-sized companies as well. He has expertise in the HR and SD modules both in the functional and technical areas, and he often acts to bridge the functional/ technical gap. You can contact Jeff at Jeff.Goldstein@SAPtips.com.

SAPtips Journal

SAPtips.com

SAPtips 2007 Klee Associates, Inc.

SAPtips Journal
February / March 2007 Volume V Issue 1
The information in our publications and on our Website is the copyrighted work of Klee Associates, Inc. and is owned by Klee Associates, Inc. NO WARRANTY: This documentation is delivered as is, and Klee Associates, Inc. makes no warranty as to its accuracy or use. Any use of this documentation is at the risk of the user. Although we make every good faith effort to ensure accuracy, this document may include technical or other inaccuracies or typographical errors. Klee Associates, Inc. reserves the right to make changes without prior notice. NO AFFILIATION: Klee Associates, Inc. and this publication are not affiliated with or endorsed by SAP AG, SAP AG software referenced on this site is furnished under license agreements between SAP AG and its customers and can be used only within the terms of such agreements. SAP AG and mySAP are registered trademarks of SAP AG. All other product names used herein are trademarks or registered trademarks of their respective owners.

Page 

SAPtips Journal

SAPtips.com

SAPtips 2007 Klee Associates, Inc.

Potrebbero piacerti anche