Monday, November 11, 2013

How to read other’s program data

When it comes to enhancing SAP functionality we usually talks about user exists, BAdIs and enhancement points. Their main purpose is to extent SAP standard functionality. As particular customer needs may differ from what is offered by standard enhancements are needed to fulfill that particular need. Therefore SAP ABAP programmers are very well known of them.

What does it take to implement the enhancement? Basically you need to find a point in standard code where you plug-in your part of enhancement code. It is not an easy to do so and it takes a lot of
debugging. What is even tricky you may get into the situation where you exactly know where to put the code but you do not have all the input information in order to produce desired output information. This is often case as input parameters of standard function modules or forms may be very limited. So some information that is needed may not be at the right place. But case can be that data is there sitting in memory just it is not part of interface of that standard functionality. Therefore you need to explore what are global data of that program module which were loaded in memory. This can be done using ABAP field symbols. Field symbol enables you to access the place in memory. It is placeholder or symbolic name for other field. It doesn't physically store the data for the field, it just points to its contents. It can point to any data object. Prerequisite is that the field symbol needs to be declared first before is assigned to the data object.

Field symbol has following declaration syntax:
FIELD-SYMBOLS <fs> [typing].

Here’s syntax for assigning Field symbol to the data object:
ASSIGN dobj TO <fs>.

Once we do not need Field symbol we can cancel assignment by following statement:
UNASSIGN <fs>.

Now let’s go back to original topic of how we can read global data from other programs. In order to read it you need to know what the ABAP program is where data object is used. Also it is necessary to make sure that a data object in that ABAP program has a value. Once we know that we prepare variable in format:

(PRG)DOBJ

Where PRG is the name of an ABAP program and DOBJ is the name of a global data object of this program. Let see in following short ABAP fragment of code:

DATAlv_data(30TYPE c.
FIELD-SYMBOLS  <> TYPE ANY.

lv_data 
'(RSAWBN_START)L_AWB_VIEW_MODE'.
ASSIGN (lv_dataTO <>.

IF <fs> EQ 'M'.
  "RSA1 was started 
ELSEIF <fs> EQ 'A'.
  "RSMON was started 
ELSEIF <fs> EQ ''.  
  "RSAWB was started 
ENDIF.  

Now if the program PROG (in this case = RSAWBN_START) is loaded during execution of the statement ASSIGN in the same internal mode as the current program, the data object (RSAWBN_START)L_AWB_VIEW_MODE is searched for in this program, and the field symbol points to this data object after the object has been successfully assigned.

This example would help us in imaginary extension of TA RSA1 of BW system. With help of this code we could possible enhanced RSA1 depending in which mode it was started. By this we would recognize in which mode RSA1 was started and we would put code into all 3 IF/ELSEIF statement.

Used sources:

1 comment:

Guillaume Garcia said...

Hi,

Tips : I sometimes resort to calling a FORM that is insignificant for the process (helper FORM of some sort) to force the loading of the main standard Program so as to be able to access its global variables "remotely".

Best regards,
Guillaume