Web Services: Make output parameter as optional
Attention: The actions on this post are done by changing the SAP standard code. Do it at your own risk.
By standard, the Web Services published in SAP can only have input parameters as optional. The option the set an output parameter as optional in the SE80 is locked.
This usually is not a problem, because the third parties adapt on there side. But in case that you really need to set an output field as optional, for example an integer field that cannot be sent empty, opening an incident to SAP will not solve your problem, because they will simply reply that it is not possible.
I make this blog to show that it is possible, but I don’t recommend sense you need to change some core SAP code.
Step 1 – Activate the “optional” option in SE80
FM: RFC_GET_FUNCTION_INTERFACE
FORM: move_if_export.
Add the following code at the beginning of the loop, after the clear.
loop at if_export. clear params. *{ INSERT IF_DOCU-FUNCNAME = 'YOURFUNCTIONMODULE' AND if_export-parameter = 'YOURPARAMETER'. params-optional = abap_true. ENDIF. *} INSERT params-paramclass = export.
The expected result after this change is that now that now, the optional flag can be selected, and if you activate it, your WSDL will be update and the output field will be set with the attribute of min occurr. equal to 0.
Step 2 – Develop a logic to define the field as null
Now that the field is optional, you need to remove it from the webservice response, depending on your scenario.
Class: CL_SRG_RFC_PROXY_CONTEXT
Method: FILL_ST_TO_XML
Inside the “loop at parameter into l_p” add your logic
loop at parameter into l_p "#EC CI_STDSEQ where direction = abap_func_importing or direction = abap_func_changing or direction = abap_func_tables. clear: l_st_p. l_st_p-name = l_p-name. l_st_p-value = l_p-value. append l_st_p to st_to_xml. clear: l_st_p. * 06.09.05vw Bug for optional changing parameter * we will send all out-parameter, regardless if the * equal named in-parameter was received or not! assign l_p-received_ref->* to <ref>. <ref> = TSOAP_TRUE. l_st_p-name = l_p-received_name. *{ INSERT ASSIGN l_p-value->* to FIELD-SYMBOL(<value>). IF ( me->function = 'YOURFUNCTION' AND l_p-name = 'YOURFIELD' AND <value> = '0' ). "Depending on the value the field is sent as NULL in the Webservice <ref> = tsoap_false. " ENDIF. UNASSIGN <value>. *} INSERT l_st_p-value = l_p-received_ref. append l_st_p to st_to_xml. endloop.