Chain - Module Pool Programming
Chain - 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 :-
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 1
CHAIN.
FIELD : ( Field1, Field2 ——— ) Module ( Module name ).
ENDCHAIN.
Requirement :-
Suppose, We are running our previous program that we have used in the
previous part.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 2
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.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 3
Step 3 :- Now execute the program and provide some incorrect order number
and click on submit button.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 4
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.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 5
But Customer wants that, the field should be enabled so that He / She can
pass the correct order number.
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.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 6
Step 6 :- Write the code into the above module.
Code :-
*&--------------------------------------------------------------
*& Modulpool ZAR_SUB_SCREEN
*&--------------------------------------------------------------
*&
*&--------------------------------------------------------------
PROGRAM zar_sub_screen.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 7
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.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 8
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.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 9
MESSAGE e001(zar_msg).
ENDIF.
ENDMODULE.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 10
We are getting the message but still the Order number field is enabled and we
can pass the correct input into it.
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 11
Using CHAIN and ENDCHAIN for Input Field Validation in SAP ABAP Module Pool Programming 12