Wednesday, 25 November 2015

Building an SAP Query with ABAP Code

ABAP code is used with SAP query tool to enhance the query output.

You can write down the code under the Extras tab for the Infoset in the SQ02 Tcode.

Building an SAP Query with ABAP Code
You will find various coding events which are similar to classical ABAP report.

Building an SAP Query with ABAP Code

The code written in this code area is transmitted to the auto generated query report. You can write down your code in various coding section as per the use of them.
  • DATA Section :
       Global Data Declaration will go under this Event.
  • INITIALIZATION Section:
       As the name suggests it is used to initialize the variable with initial values.
  • AT SELECTION-SCREEN OUTPUT:
       This event is triggered before the display of selection screen.
  • START-OF-SELECTION:
       This event is triggered before starting any database accesses. Usually Data fetching logic goes under it.
  • RECORD Processing:
        Corresponds to the GET coding with Info-sets without a logical database. As there is no hierarchical view of the data, all the join tables are addressed.
  • END-OF-SELECTION:
         The END-OF-SELECTION code consists of two parts (Before list output and After list output). The first part is processed before the list is output and the  second  part afterwards. It is the right section to do aggregated calculations and actions that can only be done when data from all the selected records has been extracted.

However, there is a problem when queries use code in the END-OF-SELECTION section in combination with the Sap List Viewer format. In short, the code is never reached.
  • Free Coding :
          The code under this event is executed after all the events are triggered.

Logic to put extra field in the query output:

Let you want a date field to be fetch from a 'B' table and has to be added to your query output.
Define a Structure in DATA Event:
data: begin of ty_date occurs 0,
                   date  like B-date,
                   end of ty_date.

The logic to fetch the values for this field will go under the record processing event.
In Record Processing Event write down the following code:
CLEAR ty_date.
CLEAR v_date.
Select date from B into table ty_date.                    "You can mention your condition also with where clause
Sort Table ty_date BY date DESCENDING.
Read Table ty_date INDEX 1.
v_date = ty_date-date.

Generate the Infoset.

Changes to the query:
1. Run SQ01 in another session.
2. Open your Query in change mode and select the field group in which has the added extra field.
3. Select extra field.
4. Click on basic list to select that field in your O/p list.
5. Click on Test button, and see if you are getting desired result.

Performing Operations on the final output table data:

You'll find table %G00 with final data to be displayed.
Enter the following ABAP code under Data Section:
    data: str type string.
   field-symbols: <G00> type table.
   field-symbols: <G00_WA> type any.

Enter the below ABAP code under END-OF-SELECTION(after list) Event:
   str = '%g00[]'.
   ASSIGN (str) TO <G00>.
   loop at <G00> assigning <G00_WA>.
   ...do whatever you like here....
   endloop.
You can put your own logic inside the loop to perform.

No comments:

Post a Comment