27
What Every BW/BI Developer Needs to Know About ABAP in BW by Sam Gassem, Senior Business Consultant/BW Lead, Rural Sourcing, Inc. This article is being made available through a partnership between SAP Community Network (SCN) and SAPexperts.com, a knowledgebase of step-by- step instruction, best practices, case studies, tools, and tips for people who support and use SAP software. Please visit our sample content page to view more SAPexperts articles. Or, visit here for information on subscribing. SAPexperts/BI Over time every BW developer needs to write ABAP in SAP NetWeaver BW to be able to meet user requirements. ABAP might be required in a start routine, end routine, expert routine, InfoPackage, Data Transfer Process, Analysis Process Designer, query, or field. Get basic knowledge on how to write ABAP code and the differences between each routine method. Key Concept Start routine is a routine in transformation that is executed before transformation is executed. End routine is a routine in transformation that is executed after transformation is executed. Expert routine is a routine in transformation that is itself the transformation. In other words it contains all three: start, end, and actual. SOURCE_PACKAGE is a structure that contains the inbound fields of the routine. RESULT_PACKAGE is a structure that contains the outbound fields of the routine. APD is a workbench with a graphical user interface (UI) for creating, executing, and monitoring analysis processes SAP NetWeaver BW provides user exits throughout the system to take advantage of custom coding add-ons and to be able to meet user requirements. After reading this article, BW developers should be able to develop an idea of when

Abap for BW

Embed Size (px)

DESCRIPTION

abap

Citation preview

Page 1: Abap for BW

What Every BW/BI Developer Needs to Know About ABAP in BW

by Sam Gassem, Senior Business Consultant/BW Lead, Rural Sourcing, Inc.

This article is being made available through a partnership between SAP Community Network

(SCN) and SAPexperts.com, a knowledgebase of step-by-step instruction, best practices, case

studies, tools, and tips for people who support and use SAP software. Please visit our sample

content page to view more SAPexperts articles. Or, visit here for information on subscribing.

SAPexperts/BI

Over time every BW developer needs to write ABAP in SAP NetWeaver BW to be able to meet user

requirements. ABAP might be required in a start routine, end routine, expert routine, InfoPackage, Data Transfer Process, Analysis Process Designer, query, or field. Get basic knowledge on how to

write ABAP code and the differences between each routine method.

Key Concept

Start routine is a routine in transformation that is executed before transformation is

executed. End routine is a routine in transformation that is executed after transformation is

executed. Expert routine is a routine in transformation that is itself the transformation. In other words it

contains all three: start, end, and actual. SOURCE_PACKAGE is a structure that contains the inbound

fields of the routine. RESULT_PACKAGE is a structure that contains the outbound fields of the

routine. APD is a workbench with a graphical user interface (UI) for creating, executing, and monitoring

analysis processes

 

SAP NetWeaver BW provides user exits throughout the system to take advantage of custom coding add-ons and to be able to meet user requirements. After reading this article, BW developers should be able to

develop an idea of when to use ABAP code using different routine methods. I show the differences

between the major methods for ABAP in the following:

1. Start routine

2. End routine3. Expert routine

4. InfoPackage

Page 2: Abap for BW

5. Data Transfer Process (DTP)

6. Analysis Process Designer (APD)

7. Query variable

8. Transformation fields

9. Start, end, and expert routines and the differences between them

NoteThis article assumes:

You have basic knowledge of SAP NetWeaver BW. You know how to create a DTP, transformations, InfoPackages, DataStore Objects (DSOs),

InfoCubes, MultiProviders, and SAP ERP Central Component (ECC) database views. You have ABAP knowledge, which is required to understand the coding parts.

ABAP in Start RoutineA start routine is used in the transformation when fetching information from other BW objects is

necessary, or when you perform calculations or other data transformations and then store them in a

global data structure or table before transformation is triggered. The start routine is run for each data package at the start of the transformation.

For example, consider a sales orders header and line items. Standard SAP objects have two transformations, one for the header and one for line items. When the two DTPs for these two

transformations are executed, two different records are created in the InfoCube. A start routine is used to

merge them into one record.

This is how it is done.

Step 1. Edit the transformation. If no start routine has been created, there is a Start Routine button to create a new one as shown inFigure 1.

Figure 1

Start a new routine

If  a start routine is already configured, a different Start Routine button is displayed to change the codes

as shown in Figure 2.

Figure 2

Change a start routine

Page 3: Abap for BW

Step 2. Developers should declare TYPES and DATA  in Figure 3 in the section labeled begin of 2nd part

global.

Figure 3

Global section, which is visible throughout the transformation

Step 3. Developers should place the body of the ABAP code  under the begin of routine section (Figure 4).

Figure 4

Start routine

 

 

Page 4: Abap for BW

NoteStart routine works with SOURCE_PACKAGE, which is an SAP built-in variable to the start routine.

ABAP in End RoutineEnd routine is executed after transformation. In other words, end routine is used to execute the post-

processing of data after transformation on a package-by-package basis. End routine is also used when

you want to perform a calculation on fields and assign fields from the start routine.

When an end routine is opened, the codes for TYPES and DATA that were entered in the global part are visible, as shown in Figure 5.

Figure 5

Global part

 

Figure 6 shows where you enter custom ABAP codes.

Figure 6

End routine section

 

Page 5: Abap for BW

To continue my example, I now populate fields from the internal table I populated in the start routine, as

shown in Figure 7.

Figure 7

End routine

 

NoteEnd routine works with RESULT_PACKAGE, which is SAP’s built-in variable to an end routine. 

Start and End Routine SummaryYou can use a start routine without using an end routine and vice versa. It depends on the requirements.

In a nutshell, a start routine without an end routine is used when you need to sort the records and delete

duplicates or delete any rows with specific criteria.

Page 6: Abap for BW

An end routine without a start routine is used when you want to do some calculations on outgoing records

before they saved to an InfoProvider. Both start and end routines are used when you need to read other

objects from BW and merge them with data read from the source system.

ABAP in Expert RoutineExpert routine is a special case routine, which means you should use it in special situations where there is no SAP built-in rule type available. 

Use this option to program the transformation without using available rule types.

Use this option when all fields in the transformation are to be affected.

Note to consider: If you already have created transformation rules, when you create an expert

routine, the system deletes them.

An expert routine is activated when you are in transformation, in edit mode, by clicking the Edit button on the menu option. Figure 8shows an expert routine.

 

Figure 8

Expert routine menu

Expert routine is used when there are not sufficient functions to perform a transformation.

Refer to Figure 9 for the routine sections.

Page 7: Abap for BW

Figure 9

Expert routine parts

 

NoteSince expert routine is both a start and end routine, it uses SOURCE_PACKAGE and RESULT_PACKAGE. As you know SOURCE_PACKAGE holds data packages coming in and RESULT_PACKAGE holds data packages going out of transformation.

When you declare an expert routine and save it, the transfomation looks something like Figure 10.

Page 8: Abap for BW

Figure 10

Expert routine transformation

You see the new Expert Routine button on the transformation (Figure 11).

Figure 11

Expert Routine button

See the sample code in "Sample Expert Routine ABAP Code" in the Code section of the BI hub of

SAPexperts.

ABAP in InfoPackageYou use ABAP in an InfoPackage when you want to program the InfoPackage to extract data from the

source using criteria where delta cannot handle it. We all know delta gets only records that have changed from the last extract. Let’s say for some reason you need to extract data starting from the last seven days

every time. You can do this using ABAP.

To enable an ABAP routine, edit the InfoPackage and under the Data Selection tab, click the pop-up icon

shown in Figure 12.

Figure 12

InfoPackage routine ABAP selection

Page 9: Abap for BW

Then click ABAP Routine (Figure 13).

Figure 13

InfoPackage ABAP Routine selection

You are prompted to enter a name. You can use any description you like (Figure 14).

Figure 14

InfoPackage routine name

Figure 15 shows sections of the routine and where you need to add code.

Page 10: Abap for BW

Figure 15

InfoPackage routine sections

Figure 16 shows sample ABAP to select yesterday’s date.

Figure 16

ABAP in InfoPackage

Page 11: Abap for BW

ABAP in DTPAs in ABAP in an InfoPackage, you use ABAP in a DTP when you want to program the DTP to extract

data from the source using criteria that delta cannot handle. Let’s say for some reason you need to

extract data starting from the last seven days every time. This can be done using ABAP.

To implement ABAP in DTP do the following.

Step 1. Edit the DTP and click the Filter button (Figure 17).

Figure 17

Filter in DTP

 

Step 2. Click the last icon (routine icon) to the right by the field you want to program (Figure 18).

Figure 18

ABAP icon in DTP

Step 3. Give the DTP a name (Figure 19).

Figure 19

DTP ABAP name

Figure 20 shows selections of the routine and where to enter ABAP code.

Page 12: Abap for BW

Figure 20

DTP ABAP sections

In Figure 21, all data from seven days ago up to today is extracted by DTP for the CALDAY field.

Page 13: Abap for BW

Figure 21

ABAP in DTP

ABAP in APDYou might be familiar with APD but, in a nutshell, APD is a workbench with a graphical user interface (UI)

for creating, executing, and monitoring analysis processes. Data can be merged from a number of sources, can go through different transformations, and can be saved to different data targets. You can

access APD by using transaction code RSANWB or by clicking the Edit button in transaction code RSA1.

Then click Analysis Process Designer in Figure 22.

Figure 22

APD menu option

NoteHow to use APD is out of scope of this article.

Page 14: Abap for BW

The following example shows how to transform and change data coming from a BEx report and then save

it to a DSO. Here the report is the source. The result of the report goes through ABAP routing and you

filter the data and finally save it into a DSO. Figure 23shows the various options you have.

Figure 23

APD available options

Once you select the query from the Data Sources options and the ABAP routine from the Transformations

options, right-click Routine 1 and then click the Properties line (not shown).

In the General tab shown in Figure 24, you give the routine a name.

Figure 24

Routine name in APD

Go to the Source fields (Source Flds) tab. From the Field List on the right panel, choose the fields you

want to extract and move them to the Source Fields on the left panel (Figure 25).

Page 15: Abap for BW

Figure 25

Source Fields selection in APD

The system then automatically fills the TargetFlds tab (Figure 26).

Page 16: Abap for BW

Figure 26

TargetFlds tab in APD

Click the Routine tab in Figure 27 and enter the ABAP code.

Page 17: Abap for BW

Figure 27

APD routine sections

Enter the code after the LOOP statement and before MOVE-CORRESPONDING as shown in Figure 27.

In my example below, I  want to change the content of the field Plant. If the data coming from the source

system is 1200 and if I want it to be A2001018, and if the content of the field plant coming from the source system is 1300 or 1600 and I want it to be different, I would use the ABAP codes shown in Figure 28.

NoteIn the routine tab  between LOOP AT and MOVE-CORRESPONDING you assign all fields from the source to the destination.

Page 18: Abap for BW

Figure 28

ABAP in APD

After checking for errors, you can then continue with the rest of APD.

ABAP in Query VariableA Query Customer Exit Variable created in a BEx query provides the means to do complex calculation (not possible with BEx) in ABAP.

Data BW queries can be restricted automatically by creating a variable customer exit. This is a two-step

process: One is to create a variable in Query Designer where it is processed by a customer exit. The

second step is to do some ABAP coding on the BW side in transaction code CMOD.

For example you want to create a variable on Posting Date. This new variable extracts data from the system using the system date with the “offset” you choose.

Page 19: Abap for BW

In the Query Designer, locate the Posting Date and right-click the option Characteristic value variables.

Then click the New Variable button (Figure 29).

 

Figure 29

Create a new variable

 

Give the new variable a technical name that starts with Z and a description. Then choose the Customer

exit option from the drop down under Processing By, as shown in Figure 30.

Figure 30

Customer exit option for variable

Save your variable by clicking on the save icon.

To write ABAP codes for the new variable just created, use transaction code CMOD in BW, which takes you to the screen shown inFigure 31.

Page 20: Abap for BW

Figure 31

Transaction code CMOD screen

Step 1. Enter the Project name. Click the Components radio button. Click the Display button.

Step 2.  To access the screen where you are able to start coding, click the green checkmark shown

in Figure 32.

Figure 32

EXIT_SAPLRRSO_001 screen

Then the system takes you to the screen shown in Figure 33.

Figure 33

Include ZXRSRU01 screen

Step 3. Double-click ZXRSRU01 as shown in Figure 33.

When you get to program ZXRSRU01, go to edit mode by clicking the pencil icon on the top left of the

screen, and implement the ABAP codes.

Page 21: Abap for BW

Another way to get to this program is to use transaction code SE38. Enter ZXRSRU01 for the program

name, and then click Edit.

 The most important information here is that the customer exit program receives the variable name of the

BW query in the ABAP variable i_vnam, which is defined as an import parameter in the function module

EXIT_SAPLRRS0_001. The BW query’s variable value is handled in the table E_T_RANGE. You can then write your ABAP code after CASE I_VNAM, starting with the keyword When. After the keyword

When, enter the technical name of the variable you created in Query Designer. Figure 34 shows ABAP

codes that start with WHEN ‘ZDAT’ and end with ENDIF. The variable ZDAT gets the value of the system

date.

Page 22: Abap for BW

Figure 34

ABAP in transaction code CMOD

Step 4. Check your work for errors and click the save icon.

Step 5. Click the activate icon to activate your codes.

Note that there are four types of SAP built-in parameter I_STEPs:

Page 23: Abap for BW

I_STEP = 1 – Call directly before the variable is entered

I_STEP = 2 – Call directly after the variable is entered. This step is activated only if the same variable

could not be filled for I_STEP = 1 and it is not ready for input.

I_STEP = 3 – In this call, you can check the values of the variables. Activating an exception (RAISE)

causes the variable screen to appear again. I_STEP = 2 is also then executed once more.

I_STEP = 0 – The enhancement is not called from the variable screen. The call can come from the

authorization check or from the monitor.

Now I am going to set the variable offset for my new variable to as many previous days as I need. For

example, in the Query Designer I set the offset to -1500  as shown in Figure 35.

Figure 35

Variable offset

 

When the report runs, the system fetches and displays the data from 1,500 days ago up to today.

ABAP in Transformation FieldsYou use ABAP in transformation fields when you want to perform a calculation on a specific single field.

You can define the routine as a transformation rule for a key figure or a characteristic. For example, in a

transformation I want to change the contents of 0DOC_TYPE.

In the transformation, right-click 0DOC_TYPE in Figure 36 and then click the Rule Details option shown

in Figure 37.  

 

Page 24: Abap for BW

Figure 36

Transformation fields

 

 

Figure 37

Rule Details

A screen pops up (Figure 38). Choose Routine from the Rule Type drop-down options.

Figure 38

Routine Rule Type

Enter ABAP codes after this section as shown in Figure 39.

Page 25: Abap for BW

Figure 39

ABAP in transformation field

NoteTransformation field works with RESULT and SOURCE_FIELDS. As you know, RESULT holds the value to be assigned to the transformation (DOC_TYPE) field, and SOURCE_FIELD holds the value from the source system.

Differences Between Each RoutineTable 1 shows a summary of when to use each routine. Also the table shows which SAP-delivered

variable is being used for each routine.

Page 26: Abap for BW

Table 1

Overview of routines

Click this link below to see a video clip of start and end routines within SAP BW transformation:

Start and End Routines in SAP NetWeaver BW Transformation

To see sample code, go to "Sample Expert Routine ABAP Code" in the BI hub of SAPexperts.