SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SAP ABAP Programming and HANA Database Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




CRM Companies List
Web Based CRM Software


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.

ABAP WebRfc Changing parameters tab

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)

ABAP WebRfc Tables parameters tab

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.

DATA ls_user TYPE soudnamei1.
DATA ls_userdata TYPE soudatai1.
DATA lv_fullname TYPE so_adrnam.

ls_user-sapname = sy-uname.

* Read SAP user fullname: http://www.kodyaz.com/sap-abap/get-fullname-of-sap-user-in-abap-code.aspx
CALL FUNCTION 'SO_USER_READ_API1'
 EXPORTING
  user = ls_user
 IMPORTING
  user_data = ls_userdata
 EXCEPTIONS
  user_not_exist = 1
  parameter_error = 2
  x_error = 3
  OTHERS = 4.
IF sy-subrc = 0.
 lv_fullname = ls_userdata-fullname. " fullname of the SAP user
ENDIF.

DATA htmldoc LIKE LINE OF html.
CONCATENATE '{"results": [ {"key": "username", "value": "' sy-uname '"}, {"key": "fullname", "value": "' lv_fullname '"}]}'
  INTO htmldoc-line.
INSERT htmldoc INTO TABLE html.

ABAP WebRFC function module code for SAP Personas script button call

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.

DATA lv_parameter_name TYPE string.
DATA lv_parameter_value TYPE string.
lv_parameter_name = 'inputParam1'.
READ TABLE query_string WITH KEY name = lv_parameter_name.
lv_parameter_value = query_string-value.

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.

SAP WebRFC service in SICF transaction

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.

call WebRFC in web browser


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.

script button calling webRfc on SAP Personas flavor

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.

SAP Personas script button Call WebRFC step

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.

display data on SAP Screen Personas flavor using WebRFC call






SAP Tutorials

SAP Tutorial

SAP Forums

SAP Tools

SAP Transaction Codes Table






Related Articles and Tutorials(http://www.kodyaz.com/sap-abap/call-webrfc-from-personas-script-to-get-user-name-and-data.aspx)
Activate SAP Personas 3.0 Services using SICF Transaction
PushOnEnter Event on SAP Personas like OnKeyPress in Javascript
How SAP Screen Personas Improve Business Value
SAP Screen Personas Set Default Selection in DropDown List
Function module is not released for the Internet
Call WebRFC from SAP Personas Script Button for User Name and Data
How to Change Background in SAP Personas MainApp
SAP Personas Html Viewer Control for Table Data
Launch Transaction Code using SAP Personas Script Button
Display Whitelist URL using SAP Personas WebPage Launch Button
Hands-on SAP Personas Tutorial for Beginners
How to Create SAP Screen Personas Flavor, Remove and Delete it
How to Export and Import SAP Screen Personas Flavors
Launch Web URL using SAP Personas Script Button
clientaccesspolicy.xml crossdomain.xml is not configured
SAP Screen Personas OnCreateHandler Script Button to Run At Page Load
Press F Key on SAP Personas Script Button for F3


Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems