Anda di halaman 1dari 19

Beginner's Guide to BI - ABAP

Applies to:
This article is applicable to all SAP BI Consultants who are novice in ABAP Skills.
For more information, visit the Business Intelligence homepage.

Summary
Routines in SAP BI are essential part as they provide a level of customization in the development. This
document provides a basic understanding on how to write these routines. We have two approaches for this,
Simple ABAP and OOPS ABAP. This document covers both the methods.
After reading this document reader should be able to design and develop ABAP Code at different levels of
SAP BI. This document will enable reader to gain basic knowledge required to develop ABAP code in BI.

Author: Sharayu Kumatkar


Company: L&T Infotech
Created on: 09 December 2009

Author Bio
Sharayu Kumatkar is a BI & ABAP Certified Associate with four years of experience as a
Senior SAP Technical consultant with L&T Infotech.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 1
Beginner's Guide to BI - ABAP

Table of Contents
Different Routines Available in SAP BI ............................................................................................................... 3
Info Package Routine ...................................................................................................................................... 3
Start Routine ................................................................................................................................................... 3
End Routine .................................................................................................................................................... 3
Important Note ............................................................................................................................................................. 4
Expert Routine ................................................................................................................................................ 5
Characteristic Routine ..................................................................................................................................... 6
Basic Coding Statements Required .................................................................................................................... 7
SELECT .......................................................................................................................................................... 7
LOOP .............................................................................................................................................................. 7
FIELD-SYMBOLS ........................................................................................................................................... 8
READ .............................................................................................................................................................. 8
SORT .............................................................................................................................................................. 8
Customer Exit Variable (BEx) ............................................................................................................................. 9
I_STEP = 0 ...................................................................................................................................................... 9
I_STEP = 1 ...................................................................................................................................................... 9
I_STEP = 2 ...................................................................................................................................................... 9
I_STEP = 3 ...................................................................................................................................................... 9
Virtual Characteristic / Key Figure .................................................................................................................... 10
Tips & Tricks ..................................................................................................................................................... 10
Debugging ..................................................................................................................................................... 10
Appendix A: Sample Code - Splitting 60+ char string into multiple IO ............................................................. 12
Appendix B: Sample code - Remove Special Characters ................................................................................ 13
Appendix C: Sample Code - OOPS Start and Characteristic Routine ............................................................. 14
Related Content ................................................................................................................................................ 18
Disclaimer and Liability Notice .......................................................................................................................... 19

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 2
Beginner's Guide to BI - ABAP

Different Routines Available in SAP BI


Info Package Routine
Info Package routine is available with Info Package of Flat file Datasource. This routine enables us to give
dynamic filename to IP.

You need to create a routine by clicking on the Routine button. Give a name to routine. Write a code inside
this routine to derive file name and assign it to variable p_filename.

Start Routine
Start Routine is available with Transformation. It is triggered before Transformation. Generally Start routines
are used for Filtering records or fetching global data to be used in Characteristic routine.

Click on Start Routine button to create Start Routine. SOURCE_PACKAGE contains contents of source data.
SOURCE_PACKAGE has same structure as that of Source of the Transformation.

End Routine
End Routine is available with Transformation. It is triggered after Transformation. Generally End user is used
for updating data based on existing data.

Click on End Routine button to create End Routine. RESULT_PACKAGE contains process data i.e.
processed via start routine and transformation. RESULT_PACKAGE has same structure as that of target
Object.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 3
Beginner's Guide to BI - ABAP

Important Note
For support pack 16 and above you get one more button besides End Routine (once end routine is created).

This button is to update behaviour of fields in End Routines. You get two options once you select this button.
One needs to make selection of proper option as it is mandatory.

The default setting for the pushbutton is that only the fields with active rules are updated in the
transformation. With this selection, fields populated in End routine wont be updated in the data target if no
active rule exists for them in Transformation.
Alternatively, you can define that all the fields should always be updated by selecting 2nd radio button. As a
result, fields filled in the end routine are not lost if there is no other active rule.

Note: Data fields with the 'No transformation' rule type in the transformation are overwritten if the transformation contains
an end routine. This problem does not occur for key figures if the aggregation summation was set in the
transformation. This problem always occurs for characteristics. If your system is below SP16 then you can
implement note 1024816 to get above facility.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 4
Beginner's Guide to BI - ABAP

Expert Routine
To create an Expert routine go to Edit menu and select Expert Routine. Expert Routine will trigger without
any transformation Rule. All existing Rules will be deleted once you develop Expert Routine. This is used
generally for customizing rules.

SOURCE_PACKAGE has same structure as that of Source of the Transformation. RESULT_PACKAGE has
same structure as that of target Object. You can manipulate values from SOURCE_PACKAGE and append
them in RESULT_PACKAGE.

Note: If the target of the transformation is a Data Store Object, key figures are updated by default with the aggregation
behaviour Overwrite (MOVE).

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 5
Beginner's Guide to BI - ABAP

Characteristic Routine
Characteristic Routine is inside Transformation. This routine will be triggered inside the Transformation
based on the characteristic or key figure. If rule is for Characteristic, then Characteristic transformation
routine will trigger. If rule is for key figure, then key figure transformation routine will trigger.

In Rule details of transformation select Routine to create a Characteristic Routine. Here you can do
calculation of InfoObject value and assign value to variable Result, which in turn will get assigned to
InfoObject. Characteristic routine is executed after Start routine and before End Routine. All global objects
declared in Start Routine are available in Characteristic Routine.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 6
Beginner's Guide to BI - ABAP

Basic Coding Statements Required


SELECT
Syntax : SELECT result
FROM source
INTO/APPENDING target
[[FOR ALL ENTRIES IN <ITAB>] WHERE <COND>]
[GROUP BY <GROUP>] [HAVING <GROUP COND>]
[ORDER BY <SORT KEY>].
Frequently used options of SELECT:
1. WHERE : The addition WHERE is used to restrict the number of records fetched by SELECT
statement. The logical expression compares the content of columns in the database with the
conditions provided in Where clause.
Terms that can be used as Logical Expressions for a WHERE Condition are
 =, EQ, <>, NE, <, LT, >, GT, <, LE, >=, GE, <=, LE
 BETWEEN
 LIKE
 IN
 IS NULL
2. FOR ALL ENTRIES : If the addition FOR ALL ENTRIES is specified before WHERE clause, then the
fields from the internal table <ITAB> can be used as operands when comparing with relational
operators. The internal table <ITAB> must have a structured line type and the component comp must
be compatible with the column col. SELECT will return records from database which are matching
with the <ITAB> based on WHERE clause.
3. INTO/ APPENDING : Addition INTO specifies where the records fetched by SELECT will get stored.
INTO clause refreshes ITAB contents and adds new records fetched by SELETC into ITAB.
Addition APPENDING will add records fetched by SELECT, at the end of ITAB. Existing contents of
ITAB remains unchanged.

LOOP
Syntax : LOOP AT <ITAB> [INTO <WORK AREA>] [WHERE <COND>].
...
ENDLOOP.
OR
LOOP AT <ITAB> [ASSIGNING <FIELD SYMBOL>] [WHERE <COND>].

ENDLOOP.
The LOOP and ENDLOOP statements define a loop around internal table records. The LOOP statement
reads lines from internal table <ITAB> sequentially. You can either read all the lines or specify conditions
<COND> to restrict which lines to be read. The statement block between LOOP and ENDLOOP is executed
once for each line.
The sequence in which the lines are read depends on the table type:
Standard tables and sorted tables : The lines are read by ascending table index.
Hashed Tables : The lines are processed in the sequence in which they were inserted in the table, and in
the sort sequence following use of the SORT statement.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 7
Beginner's Guide to BI - ABAP

The INTO clause is used to move current loop’s record into Work Area. The ASSIGNING clause is used to
assign a field symbol to current loop’s record. Field-Symbol acts as a pointer to the record. Any changes
made to field-symbol values are directly reflected in <<ITAB>, no additional Update or Modify statement is
required.

FIELD-SYMBOLS
Syntax : FIELD-SYMBOLS <FS> { typing | STRUCTURE struc DEFAULT dobj }.
The FIELD-SYMBOLS statement declares a field symbol <FS>. After its declaration, a field symbol is initial -
that is, it does not reference a memory area. You have to assign a memory area to it (normally using the
ASSIGN statement) before you can use it as an operand. Otherwise an exception/short dump will be
triggered.

READ
Syntax : READ TABLE <ITAB> { table_key | free_key | index } result BINARY SEARCH.
This statement reads a row from internal table <ITAB>. You have to specify the row by either naming values
table_key for the table key, a free condition free_key or an index. The output result determines when and
where the row contents are read.
BINARY SEARCH clause makes READ statement a bit faster. Prerequisite for using BINARY SEARCH is
that <ITAB> must be sorted with the keys used in READ’s table key.

SORT
Syntax : SORT <ITAB> [STABLE]
{ { [ASCENDING|DESCENDING]
[BY {comp1 [ASCENDING|DESCENDING] }
{comp2 [ASCENDING|DESCENDING] }
... ] } .
SORT statement sorts an internal table <ITAB>. As standard, numeric and byte-type components are sorted
by their value and character-type components according to their binary representation code page. If no
explicit sort key is entered using the BY addition, the internal table <ITAB> is sorted according to the table
key. In this case, the table key can contain a maximum of 250 components.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 8
Beginner's Guide to BI - ABAP

Customer Exit Variable (BEx)


Customer Exit Variable created in BEx query provides means to do complex calculation (not possible with
BEx) in ABAP. Coding for Customer Exit variable is done in EXIT_SAPLRRS0_001 Function Module. Data
records from BEx query are passed in I_T_VAR_RANGE internal table. Structure of this internal table is as
follows:

Field Description
VNAM Variable name
IOBJNM InfoObject name
SIGN (I)ncluding or (E)xcluding
OPT Operators: EQ =, BT Between, LE <=, LT <, GE >=, GT >, CP
and so on.
LOW Characteristic value
HIGH Upper limit characteristic value for intervals/the node-InfoObject
for hierarchy nodes.

I_STEP parameter from the Exit Function Module determines when customer exit code will be executed.
Depending upon i_step values we can populate values in variable during different steps of BEx execution.
I_STEP has values from 0-3, most commonly used values are explained below.

I_STEP = 0
When i_step has value 0, the enhancement has been called via authorization check or from the monitor and
not from variable screen.

I_STEP = 1
When i_step has value 1 the enhancement call takes place directly before variable entry. This can be used to
populate variable with some default values.

I_STEP = 2
When i_step has value 2 the enhancement call takes place directly after variable entry. This step is only
started up when the same variable is not input ready and could not be filled at I_STEP=1. This step can be
used to validate user input.

I_STEP = 3
When i_step has value 3 you can check the values of the variables. Triggering an exception here (RAISE)
causes the variable screen to appear once more. Afterwards, I_STEP=2 is also called again.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 9
Beginner's Guide to BI - ABAP

Virtual Characteristic / Key Figure


There are two ways of developing Virtual Characteristics / Key figures. One is using BADI and other is using
CMOD & SE38. If you are developing virtual KF for few Query fields then you should opt for SE38 method,
as it is very easy to maintain. But if you want virtual KF for large number of Query fields then opt for BADI
method.
For more details please refer to links provided in ‘Related Content’ section.

Tips & Tricks


Debugging
Following screenshots will explain how to debug code written in Start Routine or End Routine.
Step 1. Put a hard coded break point (BREAK-POINT) in Start Routine/End Routine which you desire to
debug.
Step 2. Open DTP of desired transformation, go to Execute tab.
Choose processing mode as ‘Serially in the Dialog Process (for Debugging)’.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 10
Beginner's Guide to BI - ABAP

Step 3. Click on change Breakpoints button in front of Transformation. Select ‘Before Transformation’ check
box to debug Start Routine. Select ‘Before End Routine’ check box to debug End Routine.

Step 4. Click on Simulate button to start debugging. Debugging will not actually load data into data target,
instead it will simulate the code behavior for Data Loading.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 11
Beginner's Guide to BI - ABAP

Appendix A: Sample Code - Splitting 60+ char string into multiple IO


Consider an example of character string of 120 Characters fetched via Datasource. As BI has character limit
of 60 for an InfoObject, we need to split this string into two InfoObjects. Following is the characteristic code
routine for these InfoObjects.
Code for InfoObject 1 Characteristic routine:
*Read first 60 characters from the string and assign to Result
MOVE SOURCE_FIELDS-STRING+0(60) TO RESULT.

Code for InfoObject 2 Characteristic routine:


*Read 60-119 characters from the string and assign to Result
MOVE SOURCE_FIELDS-STRING+60(60) TO RESULT.

Note: SAP String character numbering starts with 0.

--- Code provided by Mr. Kartik Ravi

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 12
Beginner's Guide to BI - ABAP

Appendix B: Sample code - Remove Special Characters


Most data loading related errors in BI are due to some special character in Datasource string. Following code
is helpful in avoiding such errors by removing special characters from the string.
Code:
* Assign source field to Result
RESULT = SOURCE_FIELDS-ZZLIFEX.
*Convert string into Upper case
TRANSLATE RESULT TO UPPER CASE.
*Conditional loop on String (i.e. Result) till special characters are found in string
WHILE RESULT CN
'%&()"''*+,-./:;<=>?_#~@!$^[]{}0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '.
*Replace special character found with none i.e. remove special character
* sy-fdpos -> Found location for search in byte-type and character-type data objects.
RESULT+SY-FDPOS(1) = ' '.
*Remove blank spaces from string by condensing it.
CONDENSE RESULT.
ENDWHILE.
*If first character of string is a space or special character ‘#’ then remove it.
IF RESULT(1) = '#'.
RESULT(1) = ' '.
CONDENSE RESULT.
ENDIF.

--- Code provided by Mr. Tej Trivedi

Note: Instead of above code you can also use SF_SPECIALCHAR_DELETE Function module to remove these special
characters.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 13
Beginner's Guide to BI - ABAP

Appendix C: Sample Code - OOPS Start and Characteristic Routine


These days most of the clients are asking for OOPS programming in the routine. The main purpose of OOPS
is to obtain modularity in the code. Frequently used code is converted into a class method, which can be
called from all the routines.
Following is a step by step example of basic OOPS programming used for Start Routine and Characteristic
Routine.
Step 1. Create a class using TCODE SE24

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 14
Beginner's Guide to BI - ABAP

Step 2. Create a method inside this class

Step 3. Assign parameters to the method by clicking on parameters button

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 15
Beginner's Guide to BI - ABAP

Step 4. Write code inside method

Step 5. Define class instance in global are of Start Routine

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 16
Beginner's Guide to BI - ABAP

Step 6. Create a class object in Start Routine using ‘CREATE OBJECT’ statement. Call class method in
Start Routine using above class object, pass appropriate parameters to the method

Step 7. Call class method in Characteristic Routine using above class object

---Code provided by Ms. Kshitija Karadkhedkar

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 17
Beginner's Guide to BI - ABAP

Related Content
Customer Exits on help.sap.com
Dependencies for Variables of Type Customer Exit
Update Routines and Start Routines
Routines in Transformations
Creating Virtual Characteristic/Key Figures via CMOD and SE38
Creating Virtual Characteristic/Key Figures via BADI

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 18
Beginner's Guide to BI - ABAP

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com


© 2009 SAP AG 19

Anda mungkin juga menyukai