FM can be called with specifying the name as well as passing the name to the variable and calling FM with variable name. Let me show you something interesting when you call the FM dynamically.
Once I was changing the functionality of one of the FM. This FM is dynamically called based on the conditions. The conditions and the FM name is maintained in the table. So, the FM name can be changed dynamically and more than one FM can be called for different conditions.
There were two FMs called based on different conditions. Both of them are called dynamically. So, ideally I needed to make changes in both of them. The change was to add additional Importing parameter. Based on that parameter the FM A should behave differently. So, I added the parameter into FM A only. The output was not expected.
To make it more clear, I have create these two FMs:
- ZTEST_NP_SQUARE_AREA – Get area for Square
- ZTEST_NP_RECTANGLE_AREA – Get area for Rectangle
This is simple call for both of them. Note the parameters for both of the FMs.
Demo Program to call FMs Directly
DATA: lv_len TYPE i. DATA: lv_width TYPE i. DATA: lv_area TYPE i. lv_len = '4'. CALL FUNCTION 'ZTEST_NP_SQUARE_AREA' EXPORTING iv_length = lv_len IMPORTING ev_area = lv_area. WRITE: / ' === Square === '. WRITE: / 'Length:', lv_len, / ' Area:' , lv_area. lv_width = '6'. CALL FUNCTION 'ZTEST_NP_RECTANGLE_AREA' EXPORTING iv_length = lv_len iv_width = lv_width IMPORTING ev_area = lv_area. . WRITE: / ' === Rectangle === '. WRITE: / 'Length:', lv_len, / 'Width:' , lv_width, / ' Area:' , lv_area.
If I call it like this – I get a dump – CALL_FUNCTION_PARM_UNKNOWN:
Direct FM call with unknown parameter Runtime Error
CALL FUNCTION 'ZTEST_NP_RECTANGLE_AREA' EXPORTING iv_length = lv_len iv_width = lv_width someother_param = sy-subrc " doesn't exist IMPORTING ev_area = lv_area.
Here is how the simple version of dynamic call. If you notice, I’m passing three parameters when I call the Dynamic FM using the CALL FUNCTION. FM ZTEST_NP_SQUARE_AREA has only one parameter. FM ZTEST_NP_RECTANGLE_AREA has two parameters. The third parameter doesn’t exist in any of the FM. I was expecting the Short-Dump, but no dump.
This how they are called dynamically. Note the parameters.
Dynamic FM call with Mystery Parameters
DATA: lv_len TYPE i. DATA: lv_width TYPE i. DATA: lv_area TYPE i. DATA: lv_fm_name TYPE tfdir-funcname. DO 2 TIMES. CASE sy-index. WHEN 1. lv_fm_name = 'ZTEST_NP_SQUARE_AREA'. lv_len = 4. WHEN 2. lv_fm_name = 'ZTEST_NP_RECTANGLE_AREA'. lv_len = 4. lv_width = 6. ENDCASE. CALL FUNCTION lv_fm_name EXPORTING iv_length = lv_len iv_width = lv_width someother_param = sy-subrc IMPORTING ev_area = lv_area. WRITE: / lv_area. ENDDO.
I tried to look for SAP Help for this behavior but didn’t find any.
I think it was a good idea not to give short-dump on unknown parameters. As I don’t have to make change all the FMs which would be called by that dynamic condition to have this unused parameter. If it is required, it can be simply added in the FM where it is needed. No change required to the place where the FM is called dynamically.
What do you think?
Have you faced this type of scenario before? Or any other interesting behavior with FM calls?