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 © 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

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

sdn.sap. Click on Start Routine button to create Start Routine.sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Write a code inside this routine to derive file name and assign it to variable p_filename. It is triggered before Transformation.Beginner's Guide to BI . End Routine End Routine is available with Transformation. Start Routine Start Routine is available with Transformation.com | BPX .bpx. SOURCE_PACKAGE has same structure as that of Source of the Transformation.e.ABAP Different Routines Available in SAP BI Info Package Routine Info Package routine is available with Info Package of Flat file Datasource. Generally End user is used for updating data based on existing data. Give a name to routine. RESULT_PACKAGE contains process data i. Click on End Routine button to create End Routine. RESULT_PACKAGE has same structure as that of target Object. This routine enables us to give dynamic filename to IP. Generally Start routines are used for Filtering records or fetching global data to be used in Characteristic routine.boc.com | BOC . It is triggered after Transformation. You need to create a routine by clicking on the Routine button. SOURCE_PACKAGE contains contents of source data.sap. processed via start routine and transformation.com 3 .

The default setting for the pushbutton is that only the fields with active rules are updated in the transformation. Alternatively.com | BPX . This button is to update behaviour of fields in End Routines.sap. As a result.ABAP Important Note For support pack 16 and above you get one more button besides End Routine (once end routine is created). This problem does not occur for key figures if the aggregation summation was set in the transformation.sap. fields filled in the end routine are not lost if there is no other active rule.Beginner's Guide to BI .sdn.com | BOC .com 4 . If your system is below SP16 then you can implement note 1024816 to get above facility. fields populated in End routine wont be updated in the data target if no active rule exists for them in Transformation. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.boc.bpx. One needs to make selection of proper option as it is mandatory. You get two options once you select this button. Note: Data fields with the 'No transformation' rule type in the transformation are overwritten if the transformation contains an end routine. you can define that all the fields should always be updated by selecting 2nd radio button. This problem always occurs for characteristics. With this selection.

SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BOC .boc.com 5 . Expert Routine will trigger without any transformation Rule.sap.ABAP Expert Routine To create an Expert routine go to Edit menu and select Expert Routine. You can manipulate values from SOURCE_PACKAGE and append them in RESULT_PACKAGE.sap. RESULT_PACKAGE has same structure as that of target Object. key figures are updated by default with the aggregation behaviour Overwrite (MOVE).bpx. This is used generally for customizing rules. SOURCE_PACKAGE has same structure as that of Source of the Transformation.com | BPX .Beginner's Guide to BI . Note: If the target of the transformation is a Data Store Object. All existing Rules will be deleted once you develop Expert Routine.sdn.sap.

Characteristic routine is executed after Start routine and before End Routine.com 6 .sap.com | BPX . All global objects declared in Start Routine are available in Characteristic Routine.boc.sdn. This routine will be triggered inside the Transformation based on the characteristic or key figure.com | BOC .Beginner's Guide to BI . Here you can do calculation of InfoObject value and assign value to variable Result. then Characteristic transformation routine will trigger. If rule is for Characteristic.ABAP Characteristic Routine Characteristic Routine is inside Transformation. which in turn will get assigned to InfoObject.sap.sap.bpx. If rule is for key figure. In Rule details of transformation select Routine to create a Characteristic Routine. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . then key figure transformation routine will trigger.

The LOOP statement reads lines from internal table <ITAB> sequentially. The internal table <ITAB> must have a structured line type and the component comp must be compatible with the column col. NE. EQ. <. 3.. LE BETWEEN LIKE IN IS NULL FOR ALL ENTRIES : If the addition FOR ALL ENTRIES is specified before WHERE clause.boc.com | BPX . … ENDLOOP. SELECT will return records from database which are matching with the <ITAB> based on WHERE clause. Terms that can be used as Logical Expressions for a WHERE Condition are      2.sap. LOOP Syntax : LOOP AT <ITAB> [INTO <WORK AREA>] [WHERE <COND>]. 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. GE. The LOOP and ENDLOOP statements define a loop around internal table records. =. at the end of ITAB. The statement block between LOOP and ENDLOOP is executed once for each line.bpx. OR LOOP AT <ITAB> [ASSIGNING <FIELD SYMBOL>] [WHERE <COND>]. WHERE : The addition WHERE is used to restrict the number of records fetched by SELECT statement. >=. ENDLOOP.sap.sap. <=.Beginner's Guide to BI . You can either read all the lines or specify conditions <COND> to restrict which lines to be read. .. Frequently used options of SELECT: 1. INTO clause refreshes ITAB contents and adds new records fetched by SELETC into ITAB. INTO/ APPENDING : Addition INTO specifies where the records fetched by SELECT will get stored.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>]. <>. GT. Existing contents of ITAB remains unchanged. Addition APPENDING will add records fetched by SELECT. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . <. and in the sort sequence following use of the SORT statement. The logical expression compares the content of columns in the database with the conditions provided in Where clause. >. Hashed Tables : The lines are processed in the sequence in which they were inserted in the table. LT. then the fields from the internal table <ITAB> can be used as operands when comparing with relational operators. LE.com | BOC .sdn.com 7 .

the internal table <ITAB> is sorted according to the table key.bpx. ] } .com | BOC .sap. As standard. SORT Syntax : SORT <ITAB> [STABLE] { { [ASCENDING|DESCENDING] [BY {comp1 [ASCENDING|DESCENDING] } {comp2 [ASCENDING|DESCENDING] } . a free condition free_key or an index. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com 8 . BINARY SEARCH clause makes READ statement a bit faster.. This statement reads a row from internal table <ITAB>.ABAP The INTO clause is used to move current loop’s record into Work Area. The output result determines when and where the row contents are read.. no additional Update or Modify statement is required. The FIELD-SYMBOLS statement declares a field symbol <FS>.com | BPX . the table key can contain a maximum of 250 components. a field symbol is initial that is. FIELD-SYMBOLS Syntax : FIELD-SYMBOLS <FS> { typing | STRUCTURE struc DEFAULT dobj }. Field-Symbol acts as a pointer to the record. 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. Prerequisite for using BINARY SEARCH is that <ITAB> must be sorted with the keys used in READ’s table key.boc. SORT statement sorts an internal table <ITAB>. numeric and byte-type components are sorted by their value and character-type components according to their binary representation code page.Beginner's Guide to BI .sap. If no explicit sort key is entered using the BY addition. After its declaration. The ASSIGNING clause is used to assign a field symbol to current loop’s record.sap. Any changes made to field-symbol values are directly reflected in <<ITAB>. In this case. Otherwise an exception/short dump will be triggered. You have to specify the row by either naming values table_key for the table key.sdn. READ Syntax : READ TABLE <ITAB> { table_key | free_key | index } result BINARY SEARCH.

Beginner's Guide to BI . I_STEP = 1 When i_step has value 1 the enhancement call takes place directly before variable entry. I_STEP=2 is also called again. the enhancement has been called via authorization check or from the monitor and not from variable screen. Data records from BEx query are passed in I_T_VAR_RANGE internal table.ABAP Customer Exit Variable (BEx) Customer Exit Variable created in BEx query provides means to do complex calculation (not possible with BEx) in ABAP.com | BOC .com | BPX . LT <. This can be used to populate variable with some default values. I_STEP has values from 0-3. Afterwards. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Triggering an exception here (RAISE) causes the variable screen to appear once more. BT Between. Coding for Customer Exit variable is done in EXIT_SAPLRRS0_001 Function Module. 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. Depending upon i_step values we can populate values in variable during different steps of BEx execution. Structure of this internal table is as follows: Field VNAM IOBJNM SIGN OPT LOW HIGH Description Variable name InfoObject name (I)ncluding or (E)xcluding Operators: EQ =. most commonly used values are explained below. GT >.boc. CP and so on. This step is only started up when the same variable is not input ready and could not be filled at I_STEP=1. Characteristic value Upper limit characteristic value for intervals/the node-InfoObject for hierarchy nodes.sap. GE >=.sdn.com 9 . I_STEP = 2 When i_step has value 2 the enhancement call takes place directly after variable entry.sap.bpx. LE <=.sap. I_STEP parameter from the Exit Function Module determines when customer exit code will be executed. I_STEP = 0 When i_step has value 0.

com 10 . as it is very easy to maintain. One is using BADI and other is using CMOD & SE38.sap.ABAP Virtual Characteristic / Key Figure There are two ways of developing Virtual Characteristics / Key figures.sdn. go to Execute tab.sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Step 2. Open DTP of desired transformation. Step 1.boc. For more details please refer to links provided in ‘Related Content’ section.Beginner's Guide to BI .com | BOC . Choose processing mode as ‘Serially in the Dialog Process (for Debugging)’. If you are developing virtual KF for few Query fields then you should opt for SE38 method.bpx. But if you want virtual KF for large number of Query fields then opt for BADI method.com | BPX . Tips & Tricks Debugging Following screenshots will explain how to debug code written in Start Routine or End Routine. Put a hard coded break point (BREAK-POINT) in Start Routine/End Routine which you desire to debug.sap.

Step 4.sap.com 11 .sap.sap.bpx. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Click on Simulate button to start debugging.Beginner's Guide to BI .com | BOC .ABAP Step 3. instead it will simulate the code behavior for Data Loading.sdn.boc. Select ‘Before End Routine’ check box to debug End Routine. Select ‘Before Transformation’ check box to debug Start Routine.com | BPX . Click on change Breakpoints button in front of Transformation. Debugging will not actually load data into data target.

bpx.sap. Kartik Ravi SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BOC .com 12 .Splitting 60+ char string into multiple IO Consider an example of character string of 120 Characters fetched via Datasource.sap.ABAP Appendix A: Sample Code . 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. 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. --.sdn.Code provided by Mr.com | BPX . we need to split this string into two InfoObjects.boc. Following is the characteristic code routine for these InfoObjects.sap.Beginner's Guide to BI . As BI has character limit of 60 for an InfoObject. Note: SAP String character numbering starts with 0.

CONDENSE RESULT. *Conditional loop on String (i.sap.com 13 . Tej Trivedi Note: Instead of above code you can also use SF_SPECIALCHAR_DELETE Function module to remove these special characters. *If first character of string is a space or special character ‘#’ then remove it. RESULT(1) = ' '. ENDIF.boc. RESULT+SY-FDPOS(1) = ' '.sap. IF RESULT(1) = '#'. Code: * Assign source field to Result RESULT = SOURCE_FIELDS-ZZLIFEX.Remove Special Characters Most data loading related errors in BI are due to some special character in Datasource string. *Remove blank spaces from string by condensing it.e.Code provided by Mr./:. --. CONDENSE RESULT. remove special character * sy-fdpos -> Found location for search in byte-type and character-type data objects.sdn.Beginner's Guide to BI . Result) till special characters are found in string WHILE RESULT CN '%&()"''*+. *Replace special character found with none i.e.com | BOC .<=>?_#~@!$^[]{}0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '. *Convert string into Upper case TRANSLATE RESULT TO UPPER CASE. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap. Following code is helpful in avoiding such errors by removing special characters from the string.ABAP Appendix B: Sample code .com | BPX .-. ENDWHILE.bpx.

Step 1.sap.sap. Create a class using TCODE SE24 SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BPX .com | BOC .com 14 .sap. Frequently used code is converted into a class method. which can be called from all the routines.OOPS Start and Characteristic Routine These days most of the clients are asking for OOPS programming in the routine.ABAP Appendix C: Sample Code .boc.Beginner's Guide to BI . The main purpose of OOPS is to obtain modularity in the code. Following is a step by step example of basic OOPS programming used for Start Routine and Characteristic Routine.bpx.sdn.

boc.bpx.sap.com | BPX .com | BOC .ABAP Step 2.sap.Beginner's Guide to BI .com 15 . Assign parameters to the method by clicking on parameters button SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Create a method inside this class Step 3.sdn.sap.

com | BOC .bpx. Define class instance in global are of Start Routine SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sdn. Write code inside method Step 5.Beginner's Guide to BI .sap.boc.com 16 .sap.ABAP Step 4.sap.com | BPX .

com | BPX .sap.sap.Beginner's Guide to BI .com | BOC . pass appropriate parameters to the method Step 7. Kshitija Karadkhedkar SAP COMMUNITY NETWORK © 2009 SAP AG SDN .boc. Call class method in Characteristic Routine using above class object ---Code provided by Ms.bpx.sdn. Call class method in Start Routine using above class object.com 17 . Create a class object in Start Routine using ‘CREATE OBJECT’ statement.sap.ABAP Step 6.

com | BPX .bpx.sap.Beginner's Guide to BI .boc.com 18 .ABAP Related Content Customer Exits on help.sap.com | BOC .sap.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 © 2009 SAP AG SDN .sdn.

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. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP responsible or liable with respect to the content of this document.com | BOC .bpx.boc.Beginner's Guide to BI . or seek to hold. You agree that you will not hold. code or methods suggested in this document.com | BPX . and anyone using these methods does so at his/her own risk.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.sap. including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP.sap. SAP will not be held liable for any damages caused by using or misusing the information.sdn.com 19 .sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .

Sign up to vote on this title
UsefulNot useful