Thursday, January 25, 2007

Programming with Substitution

Substitution is a very powerful tool that SAP delivers with most of the application module. Let's first see what is substitution. A substitution is a configuration step, that allows a process (or a step) of replacing values as they are being entered into the SAP R/3 system using a transaction. For example in this case, we take up the substitution use in the Work Breakdown Structure (WBS). When the users are creating or modifying a particular WBS element (using transactions CJ01 or CJ02), we can change certain values behind the scenes based on certain required logic. This values can be either for standard fields or for customer fields (CI Structures). To know more about the CI structures, please see my blog on the CI structures.

And also remember, the entered values are checked against a user-defined Boolean statement (prerequisite). If the statement is true, the system replaces the specified values. Substitution occurs before data is written to the original database.

Even though in most of the cases substitution may be simple, in some cases it could be really tricky. This document is intended to address those cases. In this example, I'm going to demonstrate a custom field to show the entire process of substitution in a WBS structure.

Displaying substitution
For the project system, we open the IMG (transaction SPRO) and navigate to the following path:

Maintain substitutions and on the left frame go to the WBS element node

Go to the menu Extras -> Substitution Fields

This will show the list of the fields that are substitutable. In the standard system, most (or all) of the table related to the WBS (PRPS) will be available. As we discussed earlier even the customer fields in the CI_PRPS structure can also be used. In my case I needed to have a bunch of Z fields and they were added to the CI_PRPS structure and in turn to the PRPS table

By adding it to the CI_PRPS structure, these fields are available for me to enhance the screen for the WBS create/change transaction. To do this you need to create a project using CMOD and include the component CNEX0007. Use the screen exit to create a screen and the Function exit to add logic. Fine, now let's go back to the substitution.
SAP has a very basic common structure for all substitution related processes. The underlying table is GB01. This table contains all the tbles and fields that can be substituted based on the class type ('S' for substitution). Here is the screen shot of table GB01.

Once we decide the substitution field, we need to insert it in to this table. SAP does not provide you any means to do that. So use the following routine to add the entries
Program: ZNFI_UPDATE_TABLE_GB01
Copy the source code, and execute the program and add the fields as required into the table GB01.

After running this report for a set of Z values, the table GB01 looks like this:

Once this is done, then the substitution structures should be regenerated. Without this step that added fields in GB01 will not ba available for substitution. To do this we need to run the SAP program RGUGBR00.

Once you complete the regeneration, let's go to the actual substitution area to create and implement one.
In the example, I've created a substitution under WBS and under the node WBS_ELM

This document assumes that you know the substitution basics and the prerequisite. In this case my prerequisite is to check the project profile before carrying out the substitution. Then an user exit code is added U0500. Double clicking on this will take you to the code. Add the code to move the values to the PRPS structure based on the WBS level.

As usual, any comments welcome.