Call WebRFC from SAP Personas Script Button for User Name and Data
In SAP Screen Personas, developer can use Script Button to call WebRFC from SAP system to send and recieve data as import export parameters of a ABAP function module. In this SAP Personas tutorial, or we can think as an ABAP tutorial on WebRfc, I will explain how ABAP programmer can create a webrfc function module and call WebRfc using a script button.
Create WebRFC Function Module
ABAP developers can create WebRFC function in the way that a function module is created using the ABAP editor. WebRFC function module is created as a normal function module, it does not require to be remote-enabled function module. So WebRFC does not mean remote RFC.
To demonstrate how script button is used to call WebRFC from a SAP Personas flavor, in this tutorial I'll share source codes of a WebRFC which returns current SAP username and user's fullname.
To make it easier, call SE37 Function Builder transaction code as the initial start point.
I gave the name "ZEY_Personas_UName" for my sample WebRFC.
WebRFC function module does not use Import and Export parameters, instead it uses Changing and Tables parameters for parameter value exchange during calls. So leave import and export parameters tabs unchanged.
In "Changing" tab make parameter declarations as follows:
Content_Type Like W3Param-Cont_Type with Default value as 'application/json'
Content_Length Like W3Param-Cont_Len
Return_Code Like W3Param-Ret_Code
Among these W3Param parameters, CONTENT_TYPE stands for HTML content type.
CONTENT_LENGTH is for HTML content length and RETURN_CODE is for Return value of a Web-callable function module.
These parameters are for webRfc usage from web and sets related HTTP protocol related data. ABAP programmers will not make any change on these parameters.
The Tables tab for table type parameters is actually the place where we read input values using Query_String parameter. And return or export parameters in HTML format using HTML or MIME table type parameters.
Please define following parameters as written below:
Query_String Like W3Query (query string for WWW server),
HTML Like W3HTML (contains HTML for WWW client),
MIME Like W3MIME (contains MIME data for WWW client)
Below ABAP developers can find the full source code of WebRFC or function module implementing a special interface which enables SAP users to call and execute ABAP function module from web.
Please note that especially for the ABAP programmers, above ABAP function module as a WebRFC does not read any input parameter from the query string.
If the application passes input parameters using the query string attached at the end of the URL, to read the query string parameters following code can be used.
Before calling WebFRC from internet like using it on a SAP Personas Call WebRfc script button step, ABAP programmer has to release the function module to internet using SMW0 transaction. Otherwise users will experience Function module is not released for the Internet error message. Please refer to ABAP tutorial how to release function module to internet to prevent this SAP error on your applications.
Activate WebRFC Service using SICF Transaction
Before you call ABAP WebRFC from a web browser for test, be sure that the service "webrfc" is active.
You can check the activation status of webrfc by calling "SICF" maintain services transaction and following the given path:
/default_host/sap/bc/webrfc - Service for Emulation of ITS Web RFC Service
If the service is not active, Activate Service by using context menu with a right-click on the webrfc node.
To test WebRFC on a web browser to see how WebRFC is called and what is the output in HTML tables parameter, you can call the following URL but with your system variables. If you named your WebRFC using a different name, replace the part after "_function=" which is the key for function module.
http://sap01k0d.kodyaz.com:8081/sap/bc/webrfc?_function=ZEY_PERSONAS_UNAME&sap-client=260
As ABAP developers will see immediately, the output as HTML is in .json format keeping key-value pairs of data.
Edit SAP Flavor Script Button for WebRFC Call and Display Data
On SAP Screen Personas flavor, using the Manage Flavor icon switch to Edit mode and place two text boxes and a script button on to the layout.
Open the script button to add the required steps that will call webRfc behind and read returned data from WebRfc then display on text boxes.
WebRfc returns following data which is in .json format:
{"results": [ {"key": "username", "value": "YILMAZ-E"}, {"key": "fullname", "value": "Eralper Yilmaz"}]}
As you can see the keys are username and fullname, and json data provides corresponding values. These values are reachable easily from script button "Paste Value" scripts.
Let's start scripting now in SAP Personas' way.
First step is Call WebRFC. Provide the WebRFC URL address to the script step.
Second step is to read the fullname value from WebRFC returned .json data and set it as text value of the textbox.
This step reads fullname key value from .json result data set and links the fullname value to the text box.
Thirst step is similar to second one with one difference. This steps reads the username key value and links the value to the other textbox.
That was all for the SAP Personas script button executing the WebRFC and displaying key values on text boxes placed on the flavor. When I press the script button titled "Get Current User" the outputs sy-uname and full name of the SAP user is displayed on textboxes.