ABAP: how to use extra parameters when calling fuctions and reduce redundancy?
I have an ABAP-OO class where I want to call a function module inside the foo () method. There are two cases (A and B) where I have to use the foo () method. Let's say the default value is used for case A, and to use this function module:
" importing something something_else
METHOD FOO.
CALL FUNCTION 'A_FUNCTION'
EXPORTING
required_param_x = something
required_param_y = something_else
* optional_param = " i am commeted out and only need for case B
.
ENDMETHOD.
Case B is "special" and also requires the option_param to be set.
My current situation is the second way:
" importing something something_else case_b_stuff
METHOD FOO_B_CASE.
CALL FUNCTION 'A_FUNCTION'
EXPORTING
required_param_x = something
required_param_y = something_else
optional_param = case_b_stuff
.
ENDMETHOD.
Of course, this is very redundant. My real encoding is also much more complex than shown above. My question is, how can I get rid of this foo_b_case () method and make foo () fit for both?
Suppose I make the "case_b_stuff" parameter optional and just pass it in each case. How does ABAP handle "optional_param" if "case_b_stuff" is original?
source to share
as already explained in the comment, first check what the function module has for this optional parameter. If it's not there, send the seed value ok. If there is a default, sending an initial value will override the default behavior. To ensure that FM works as expected, initialize your parameter variable to the default for FM, and override it if necessary.
source to share
For dynamic selection of function module parameters, see PARAMETER-TABLE . Unfortunately this does not work for RFC calls, but for local calls, you can use it along with a condition IS SUPPLIED
.
IF case_b_stuff IS SUPPLIED.
"add the parameter to the table to be included in PARAMETER-TABLE call
ENDIF.
CALL FUNCTION 'A_FUNCTION'
PARAMETER-TABLE
lt_parameter
EXCEPTION-TABLE
lt_exception.
Thus, you can only have one method foo
. Whether the body of this method will be neat is another question. This is because ABAP does not allow method names to be overloaded like in Java or C ++.
source to share