Top Banner
Beginners Guide to ABAP in BW Presented by Karen Dawson
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ABAP

Beginners Guide to ABAP in BW

Presented by Karen Dawson

Page 2: ABAP

InfoSession

� The objective of this InfoSession is to enable participants to gain basic knowledge required

to develop ABAP code in BI.

Page 3: ABAP

• Understand basic principles.• Using the editor - see function module Z_GET_SPARK_VERSION_ATTRIBUTES• Table types• Transformation (Start, End, Expert Routines)• Performance issues• Tips and Tricks• Useful Transactions

What is covered in today’s session

• ABAP in the Extraction Process• ABAP in InfoPackage Routine• OOPS ABAP.

Not covered today’s session

Agenda

Page 4: ABAP

Not covered in this InfoSession:

� ABAP in the Extraction Process

� ABAP in InfoPackage Routine

� OOPS ABAP.

Not covered here4.

Page 5: ABAP

� Different Routines available in SAP BI• Start Routine

• End Routine

• Expert Routine

• Characteristic /Key Figure Routine

• Customer Exit Variable (Bex)

• InfoPackage (not covered in this presentation)

• Process Chain ABAP (not covered in this presentation)

ABAP in BI

Page 6: ABAP

Global Data / Data Dictionary

To work with data at runtime, you need to store it in the program at runtime and address it from your program. The system needs to know the type of the data (for example, character string, whole number, table consisting of name, currency amount, and date, and so on).

Between *$*$ begin of global ... and *$*$ end of global ... you can define global data declarations 'CLASS-DATA' that are then available in all routines.

Structure definition

A structure is a user-defined sequence of data types. It fully defines the data object. You can either access the entire data object, or its individual components. You need to define your own structures, either in the ABAP program in which you want to use it, or in the ABAP Dictionary.

Go to Data Dictionary – data type

Use the following statement

*$*$ begin of global - insert your declaration only below this line *-*... "insert your code here

TYPES: BEGIN OF t_cust,/bic/zcnf_cust TYPE /bic/oizcnf_cust,/bic/zcustl_06 TYPE /bic/oizcustl_06,

END OF t_cust.**

DATA: l_t_custattr TYPE HASHED TABLE OF t_cust WITH UNIQUE KEY/bic/zcnf_cust.

*$*$ end of global - insert your declaration only before this line *-*

Page 7: ABAP

Internal Tables in ABAP

Internal Tables

Internal tables consists of a series of lines that all have the same data type.

Internal tables are characterised by:

� Their line type. The line type of an internal table can be any ABAP data type - an elementary type, a structureor an internal table.

� A key. The key of an internal table is used to identify its entries. It is made up of the elementary fields in theline. The key may be unique or non-unique.

� The access type. The access method determines how ABAP will access individual table entries.

There are three access types, namely unsorted tables, sorted index tables and hash tables

Page 8: ABAP

Choosing a Table Type

Hashed tablesThis is the most appropriate type for any table where the main operation is key access. You cannot access a hashedtable using its index. The response time for key access remains constant, regardless of the number of table entries.Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct anduse an internal table which resembles a database table or for processing large amounts of data.

Standard tables

This is the most appropriate type if you are going to address the individual table entries using the index. Index access is thequickest possible access. You should fill a standard table by appending lines (ABAP APPEND statement), and read, modifyand delete entries by specifying the index (INDEX option with the relevant ABAP command). The access time for a standardtable increases in a linear relationship with the number of table entries. If you need key access, standard tables areparticularly useful if you can fill and process the table in separate steps. For example, you could fill the table by appendingentries, and then sort it. If you use the binary search option with key access, the response time is logarithmically proportionalto the number of table entries.

Sorted tablesThis is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERTstatement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries arerecognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional tothe number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partiallysequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.

Page 9: ABAP

Structures

You use structures in ABAP programs to group work areas that logically belong together. Since the individual elements within a structure can be of any type, and can also themselves be structures or internal tables, the possible uses of structures are very wide-ranging. For example, you can use a structure with elementary data types to display lines from a database table within a program. You can also use structures containing aggregated elements to include all of the attributes of a screen or control in a single data object.

Use the following statement

*----------------------------------------------------------------------** Method start_routine*----------------------------------------------------------------------** Calculation of source package via start routine*----------------------------------------------------------------------** <-> source package*----------------------------------------------------------------------*

METHOD start_routine.*=== Segments ===

FIELD-SYMBOLS:<SOURCE_FIELDS> TYPE _ty_s_SC_1.

DATA:MONITOR_REC TYPE rstmonitor.

SELECT /bic/zcnf_cust* /bic/zcustl_03* /bic/zcustl_04* /bic/zcustl_05

/bic/zcustl_06FROM /bic/mzcnf_custINTO TABLE l_t_custattrWHERE objvers = 'A' ANDdateto = '99991231' ANDsoursystem = 'RU'.

*$*$ end of routine - insert your code only before this line *-*ENDMETHOD. "start_routine

Page 10: ABAP

Where ABAP is used

Subroutines

You call subroutines from ABAP programs using the PERFORM statement.

Subroutines are introduced with the FORM statement and concluded with the ENDFORM statement.

You can define subroutines in any ABAP program. You can either call a subroutine that is part of the same program or an external subroutine, that is, one that belongs to a different program. If you call an internal subroutine, you can use global data to pass values between the main program and the subroutine. When you call an external subroutine, you must pass actual parameters from the main program to formal parameters in the subroutine.

Function Modules

Function modules are external functions with a defined interface. You call function modules from ABAP programs using the CALL FUNCTION statement.

Function modules are introduced with the FUNCTION statement and concluded with the ENDFUNCTION statement.

You can only create function groups within special ABAP programs called function groups using the Function Builder. This means that you can only call them externally from other ABAP programs. Unlike subroutines, you always pass data to function modules using an explicit parameter interface.

Methods

Methods describe the functions of classes in ABAP Objects. Like function modules, they have a defined interface. You call methods from ABAP programs using the CALL METHOD statement.

Methods are introduced with the METHOD statement and concluded with the ENDMETHOD statement.

Methods can only be defined in the implementation parts of classes. You can either do this globally in the Class Builder, or locally within ABAP programs. Methods can work with the attributes of their class and with data that you pass to them using their explicit parameter interface.

Page 11: ABAP

DATA: DAYSOLD TYPE P,

DOB TYPE D,

TODAY LIKE SY-DATUM.

DOB = ‘19621230’.

TODAY = SY-DATUM.

DAYSOLD = TODAY - DOB.

WRITE: ‘You are’, DAYSOLD, ‘days old.’.

You are 12410 days old.

Date fields store values as YYYYMMDD.

Date Calculations in ABAP

Page 12: ABAP

DATA: NUM TYPE I VALUE 12.

FIELD-SYMBOLS: <F1>,

<F2> TYPE I,

<F3> LIKE NUM.

ASSIGN: NUM TO <F1>,

NUM TO <F2>,

NUM TO <F3>.

WRITE: / ‘Line 1:’, NUM, <F1>, <F2>, <F3>.

<F1> = 32.

WRITE: / ‘Line 2:’, NUM, <F1>, <F2>, <F3>.

A field symbol is a

“pointer” that

assumes a field’s

address, not a field’s

value.

Line 1: 12 12 12 12

Line 2: 32 32 32 32

Working with Field Symbols in ABAP

The “ASSIGN”statement associates a field to a field symbol at runtime (i.e., when the program is executed).

Page 13: ABAP

Data Dictionary

� A data dictionary is a centralized storage location for information about the data that is stored in a database.

� This information is often called “metadata” (data about data).

� SAP’s data dictionary is called the ABAP Dictionary.

� A data dictionary provides answers to questions such as:

� What data is contained in the database?

� What are the attributes of this data: name, length, format, etc.?

� What relationships exist among different data objects?

Page 14: ABAP

How to decide on Start, End, etc

� How to decide whether to use Start Routines, End Routines, Individual Update or

Expert Routines

� Start Routine– Generally Start routines are used for Filtering records or fetching global data to be used in Characteristic routine.

– Start Routines are preferred when any transformation is needed in the data at package Level

� Individual Routine– Individual Routines can be used to read data from the global Internal Table which is populated in the start routines or to

implement any simple formula or a calculation

� End Routine– End Routines are Target Specific. Any fields which you wish to populate which are not present in the souce but present

in the target, can be filled using End Routines. End routine alone can replace start routines as well as individual routines in some cases.

� Expert Routine– The expert routine completely replaces the graphical modelling process and enables you to convert each data package

from the source into the target structure in a single routine.

Page 15: ABAP

Transformation

� The transformation contains a total of five abap exits1. Exit for determining key figures and data fields

2. Exit for determining characteristics and key fields

3. Start Routine

4. End Rountine

5. Expert Routing

Page 16: ABAP

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

� The start routine is run for each data package at the start of the update.

� The start routine has only these parameters: MONITOR, MONITOR_RECNO, DATA_PACKAGE, RECORD_ALL, SOURCE_SYSTEM and ABORT. DATA_PACKAGE refers to the entire data package.

� The start routine does not have a return value. It is used to perform preliminary calculations and store these in a global data structure or in a table. You can access this structure or table from other routines.

Page 17: ABAP

Start Routine ExampleUse the following statement

*$*$ begin of routine - insert your code only below this line -* fill the internal tables "MONITOR" and/or "MONITOR_RECNO",* to make monitor entriesTABLES: /bic/AZOLAMTOT00.

DATA : lt_ZOLAMTOT like /bic/aZOLAMTOT00 occurs 0 with header line.

SELECT * from /bic/aZOLAMTOT00 into table lt_ZOLAMTOTfor all entries in data_packagewhere /bic/aZOLAMTOT00-/bi0/company = data_package-/bi0/companyand /bic/aZOLAMTOT00-/bi0/chrt_accts = data_package-/bi0/chrt_acctsand /bic/aZOLAMTOT00-/bi0/gl_account = data_package-/bi0/gl_accountand /bic/aZOLAMTOT00-/bi0/ref_key3 = data_package-/bi0/ref_key3AND /bic/aZOLAMTOT00-ztotal 0.

sort lt_zolamtot by /bi0/company /bi0/chrt_accts /bi0/gl_account /bi0/ref_key3.

DELETE ADJACENT DUPLICATES FROM lt_zolamtot comparing /bi0/company /bi0/chrt_accts /bi0/gl_account /bi0/ref_key3.

loop at data_package.read table lt_zolamtot with key

/bi0/company =data_package-/bi0/company /bi0/chrt_accts=data_package-/bi0/chrt_accts/bi0/gl_account=data_package-/bi0/gl_account/bi0/ref_key3=data_package-/bi0/ref_key3

BINARY SEARCH.if sy-subrc ne 0.

delete data_package.else.

data_package-/bic/ztotal = lt_zolamtot-/bic/ztotal.data_package-/bi0/loc_currcy = lt_zolamtot-/bi0/loc_currcy.

endif.endloop.

* if abort is not equal zero, the update process will be canceledABORT = 0.

Page 18: ABAP

Characteristic Routine

Characteristic Routine is inside the Transformation.

This routine will be triggered inside the Transformation based on the characteristic or key figure.

Characteristic routine is executed after Start routine and before the End Routine. All global objects declared in Start Routine are available in Characteristic Routine.

Page 19: ABAP

End RoutineEnd Routine is available with Transformation.

It is triggered after Transformation.

Generally End user is used for updating data based on existing data.

The End Routine clearly mirrors the start routine, the only difference in the code is that the name SOURCE_PACKAGE is RESULT_PACKAGE.

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.

Page 20: ABAP

Expert Routine

Using an Expert Routine has advantages for system performance.

If you need a Start Routine and an End Routine then use an Expert Routine instead.

Page 21: ABAP

Expert Routine

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

�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.

Page 22: ABAP

End Routine Example

Click below to view an example End Routine

End Routine Sample

Page 23: ABAP

Performance tips

ABAP for a BI Consultant is usually limited to implementing the logic and making it work out.

BI Consultants are rarely concerned about the performance while implementing the Business Logic in Routines.

The consequences of these are faced by the Support Team who don’t understand why the Data load is taking so much time in Production.

So for a successful BI Implementation it is mandatory that we also consider the performance aspect while writing ABAP Logic.

Performance does matter when it comes to loading large volumes of data in the Production System.

This document gives you an overview of the best practices which should be followed while writing an ABAP Code considering the performance.

Page 24: ABAP

Performance tips

Deleting a Record

Sometimes you might come across a requirement when you want to delete records for multiple values.

Eg. I would like to delete all records from source_package where F1 = ABCD and EFGH.

In that case you can use the SELECT-OPTIONS type.

Range tables provide an easier method to either add or remove values we want.

Don’t use this

Loop at source_package assigning <source_fields>. If <source_fields>-F1 = ‘X’. Delete source_package. Endif. Endloop.

Use the following statement

Delete source_package where F1 = ‘X’.

Use the following statement

DATA : TEMP type F1. “i.e. the field for which we are creating SELECT-OPTIONS SELECT-OPTIONS : s_F1 for temp. DATA : wa like line of s_F1. wa-sign = 'I'. wa-option = 'EQ'. wa-low = 'ABCD'. append wa to s_F1. wa-sign = 'I'. wa-option = 'EQ'. wa-low = 'EFGH'. append wa to s_F1. Delete source_package where F1 in s_f1.

Page 25: ABAP

Sample Code # 1

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.

Page 26: ABAP

Sample Code #2Sample code - Remove Special Characters

Most data loading related errors in BI are due to some special character in the Datasource string. Following code is helpful in avoiding such errors by removing special characters from the string.

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

Use the following statement* 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.

Page 27: ABAP

Useful Transactions

� SE38

� SE37

� SM50

� SM04

� SM12

� SE11

� SE15

� SE16

� SM66

� BAPI

Useful ABAP Programs & Function Modules in SAP BI -http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=35458

Page 28: ABAP

Questions?

This presentation should provide the participant with a basic knowledge required to develop efficient ABAP code in BW.