Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming

 In SAP ABAP programming, CHAIN and ENDCHAIN are control statements used within module pools to iterate over a set of records and perform operations on them. Here's how they work:

  • CHAIN: The CHAIN statement is used to begin a loop that iterates over a set of records in a specified internal table. It's often followed by FETCH NEXT CURSOR statement to fetch the next record in the internal table. Each iteration of the loop processes one record at a time.
  • ENDCHAIN: The ENDCHAIN statement marks the end of the loop initiated by CHAIN. It's used to close the loop block.

Purpose :-

  • The Purpose of CHAIN and ENDCHAIN is to validate the field input.
  • Usually in Input screen, Whenever the user gives an input and there is no record present, User input is restricted by an error. This error message disables the input screen and now the user cannot provide correct values in the input field as it is disabled by the message.
    • To overcome this issue, Chain and ENDCHAIN comes into the flow logic of the screen where the input fields are validated and remain enabled after error message.

Syntax :-

  • CHAIN.

    FIELD : ( Field1, Field2 ——— ) Module ( Module name ).

    ENDCHAIN.


Requirement :-

  • Suppose, We are running our previous program that we have used in the previous part.



  • Suppose, I am passing a wrong order number and now What I want is that the System should give a message that Order Number is not correct.


Solution :-

  • Step 1 :- Go to se91 and write a message in message class.



  • Step 2 :- Open the PAI logic of our program.



  • Step 3 :- Now execute the program and provide some incorrect order number and click on submit button.



    • Note :- We are getting the message as Order number is not correct.
    • But there is a measure problem with the code, the input field of order number is now disabled and we cannot pass any input to it.
    • But Customer wants that, the field should be enabled so that He / She can pass the correct order number.
    • So, for that purpose, we will use Chain and ENDCHAIN.

  • Step 4 :- Open the flow logic of our screen, write the below code for CHAIN and ENDCHAIN.



  • Step 5 :- Double click on the Validate → It will give a popup → Click on yes → Select master program and press enter.



  • Step 6 :- Write the code into the above module.




Code :-

*&---------------------------------------------------------------------*
*& Modulpool ZAR_SUB_SCREEN
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
PROGRAM zar_sub_screen.

TABLES : zar_header.
TYPES : BEGIN OF ty_header,
          order_number TYPE zar_order_number,
          order_date   TYPE zar_order_date,
          payment_mode TYPE zar_payment_mode,
          total_amount TYPE zar_total_amount,
          currency     TYPE zar_curency,
        END OF ty_header.

DATA : lt_header TYPE TABLE OF ty_header,
       ls_header TYPE ty_header.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'HEADER'.
  SET TITLEBAR 'HDR'.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  IF sy-ucomm EQ 'SUBMIT'.
    SELECT order_number order_date
      payment_mode total_amount currency
      FROM zar_header INTO TABLE lt_header
      WHERE order_number = zar_header-order_number.

    IF lt_header IS NOT INITIAL.
      READ TABLE lt_header INTO ls_header INDEX 1.
      IF sy-subrc EQ 0.
        zar_header-order_date = ls_header-order_date.
        zar_header-payment_mode = ls_header-payment_mode.
        zar_header-total_amount = ls_header-total_amount.
        zar_header-currency = ls_header-currency.
      ENDIF.

    ENDIF.
  ENDIF.

  IF sy-ucomm EQ 'BACK'.
    LEAVE TO SCREEN 0.
  ENDIF.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  EXIT  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE exit INPUT.
  IF sy-ucomm EQ 'CANCEL'.
    LEAVE TO SCREEN 0.
  ENDIF.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  VALIDATE  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE validate INPUT.
  SELECT order_number order_date
        payment_mode total_amount currency
        FROM zar_header INTO TABLE lt_header
        WHERE order_number = zar_header-order_number.

  IF lt_header IS INITIAL.
    MESSAGE e001(zar_msg).
  ENDIF.
ENDMODULE.

Execute the code :-



  • Provide a wrong input and click on submit button.


  • We are getting the message but still the Order number field is enabled and we can pass the correct input into it.



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