Mastering ABAP Events: Unraveling Interactive Classical Report Events and Control Break Statements

 Welcome back everyone, We were discussing about Interactive Classical Report Events, So let’s continue it.


3. Top of Page During Line-Selection :-

  • This event calls when the first WRITE statement occurs in a program for a page on secondary list.
  • The purpose of this event is to provide title/header at the beginning of a new page on secondary list.

Let’s take a requirement to understand it more clearly.

Requirement :-

  • I want on the first page my header details should be displayed and on the second page item details should be displayed.
  • Also there should be a title for the secondary list.


Code :-

*************************************************************
*Start of Program
*Declaring Strcutrure for header
TYPES: BEGIN OF ty_header,
         order_number TYPE zar_order_number,
         order_date   TYPE zar_order_date,
         payment_mode TYPE zar_payment_mode,
         currency     TYPE zar_curency,
       END OF ty_header.

*********************
*Internal table and Work Area
DATA : lt_header TYPE TABLE OF ty_header,
       ls_header TYPE ty_header.

****************
*Declaring Strcuture for item table
TYPES: BEGIN OF ty_item,
         order_number      TYPE zar_order_number,
         order_item_number TYPE zar_order_item_number,
         item_cost         TYPE zar_total_amount,
       END OF ty_item.

************************************
*Declaring internal table and work area
DATA : lt_item TYPE TABLE OF ty_item,
       ls_item TYPE ty_item.

*******************************
*Variable declaration.
DATA : lv_field(30) TYPE c,
       lv_value(30) TYPE c.
*******************************
*Select option Declaration
DATA : lv_order TYPE zar_order_number.
SELECT-OPTIONS : s_order  FOR lv_order.

*********************************
*Writing a select query for header
START-OF-SELECTION.
  SELECT order_number order_date
    payment_mode currency
    FROM zar_header INTO
    TABLE lt_header
    WHERE order_number IN s_order.

***************************************
*Displaying the data
  LOOP AT lt_header INTO ls_header.
    WRITE :/ ls_header-order_number,
             ls_header-order_date,
             ls_header-payment_mode,
             ls_header-currency.

  ENDLOOP.
*********************************
AT LINE-SELECTION.
***********
*GET Cursor Declaration.
  GET CURSOR FIELD lv_field VALUE lv_value.
*******************
*Using stored data in GET CURSOR.
  IF lv_field = 'LS_HEADER-ORDER_NUMBER'.
    SELECT order_number
        order_item_number
        item_cost FROM zar_item
      INTO TABLE lt_item
      WHERE order_number = lv_value.

    IF lt_item IS NOT INITIAL.
****************************
*LOOP on item
      LOOP AT lt_item INTO ls_item.
        WRITE :/ ls_item-order_number,
                 ls_item-order_item_number,
                 ls_item-item_cost.
      ENDLOOP.
    ENDIF.
  ENDIF.

*******************************
*Title on Basic List.
TOP-OF-PAGE.
WRITE :/ 'Header Details'.

*****************************
*TITLE for the Secondary List
TOP-OF-PAGE DURING LINE-SELECTION.
WRITE :/ 'Item Details'.

********************************************
*End of Program
***************************************************************

Execute the Program :-

1. Basic List



double click on any row.

2. Secondary List




Control Break Statements :-

  • Control break statements are also called as control break events.

  • Control break statements are used to control loop or we can say they are used to control the data flow in loop.

  • Control break statements starts with at and end with endat.


Pre-requisites to apply control break statements :-

  • Control break statements should be applied inside a loop.
  • Internal table should be in the sorted order.

Types of Control Break Statements :-

  • The various control break statements are as follows :-
    • AT FIRST :- It triggers for the first row/first iteration of the internal table in loop.
    • AT LAST :- It trigger for the last row/last record/last iteration of the internal table in loop.
    • AT NEW <fieldname> :- It triggers for the new/first record of a group having the same value for the specified field name in loop.
    • AT END OF <fieldname> :- It triggers for the last record of a group having the same value for the specified field name in loop.

Control Break Statements with SUM Statement :-

  • SUM statement can only be used in loop-end loop statement and needs to be applied in control break statements (at and endat).
  • SUM calculates the sum of all numeric fields to the right of the current group key.
  • The sum is for all rows of the current group and assign it to the corresponding component of work area.

Let’s understand the Control break statements with a requirements.

Requirements :-

  • Suppose, this is our order header table.



  • and this, is the data present in it.



  • Now, what Customer wants is, Based upon the payment mode, I should be displaying the Total Amount.

Solution :-

  • Step 1 :- Create a executable program in ABAP Editor.

  • Step 2 :- Declare internal table and work area.



  • Step 3 :- Write Select Queries



  • Step 4 :- Write Control break statements inside the loop.




Code :-

****************************************************************
*Start of Program
**********************************
*Type Structure declaration
TYPES : BEGIN OF ty_data,
          payment_mode TYPE zar_payment_mode,
          total_amount TYPE zar_total_amount,
        END OF ty_data.
***************************************
*Internal table and work area declaration
DATA : lt_data TYPE TABLE OF ty_data,
       ls_data TYPE ty_data.

*************************
*Select options
DATA : lv_pm TYPE zar_payment_mode.
SELECT-OPTIONS : s_pm FOR lv_pm NO INTERVALS.

*******************************************
*Writing Select Queries
SELECT payment_mode total_amount
  FROM zar_header
  INTO TABLE lt_data
  WHERE payment_mode IN s_pm.

IF lt_data IS NOT INITIAL.
  SORT lt_data BY payment_mode.
*************************************
*Implementing Control Break  statements
  LOOP AT lt_data INTO ls_data.
    AT FIRST.
      WRITE :/ 'The Sum based Upon Paymenr mode is as follows'.
    ENDAT.

    AT NEW payment_mode.
      WRITE :/ ls_data-payment_mode.
    ENDAT.

    AT END OF payment_mode.
      SUM.
      WRITE : ls_data-total_amount.
    ENDAT.

    AT LAST.
      WRITE :/ 'End of Collection'.
    ENDAT.
  ENDLOOP.
ENDIF.

*****************************************
*End of Program
************************************

Execute the program :-



  • Here, you can compare the output with the data given in the requirement.
  • We are getting sum of total amount for corresponding payment mode.

So, that’s enough for today we will continue our journey in the next blog.

Thanx a lot for being a part of this wonderful journey.

Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming