Additional Blogs by Members
Showing results for 
Search instead for 
Did you mean: 
Active Participant

Hello SCN friends,

Today I want to share with all of you a simple integration that I made with a third party e-learning system using restful approach with json, before I enter in technical details I want to say in general terms what I did:

The first part of the requirement was creating courses in the third party e-learning system from SAP, this had to be made from HCM events management module and after that, synchronize this events to the e-learning system on demand in a massive way using a Z program.

The second part was synchronizing the students inscribed in e-learning system course to SAP system, in SAP this data had to be received in HCM events management and inscribe the students in event previously synchronized to e-learning system (course = event).

This blog doesn’t intend show functional details of the solution, the only objective is to show you technical details of using restful and json. So feel free to ask me about functional details if you want.

Part 1.

Only for information, the e-learning system is developed in phyton, but that is not relevant because all we have to know is restful approach and how to implement it in SAP. In this part I develop a program to send event to e-learning system, in the following code snippet extracted from the program I will show you how to send information to the e-learning system using POST method with an entity in json format that contains the course data to be created in remote system:

The variables declaration part is this:

First I transform the data to json format using transformation id, this automatically converts the parameters to a json object (lo_json_writer).

After that, i create a http object with the url that I want to connect:

Then I prepare the http request entity with json object containing data for the course creation in the external system, first I instantiate object mo_rest_client to handle communication with restful service, after that, I call method create_request_entity to create request object, set content type to json and set the data to request object, calling set_binary_data method, as a parameter I pass the result of the calling to get_output method of lo_json_writer, in this point it specifies the data of the course in json format and puts it in the request object. At calling to post method, passing the entity built it before it makes the call to the service. With the get_status and get_respose_entity it returns the http response code and status for example code 200 status OK.

The external service returns a json object as a response, I get this response with method get_string_data of the lo_rest_entity object, after that I convert this json format data to Abap variables using transformation id. Note that I don’t have to create a transformation program, the transformation is automatically made using transformation id call.

Part 2.

When the user logged into the external elearning system clicked in some option (button) to send students that finished the course to SAP to be inscribed, so this part is made to respond this call. The goal in this part of the requirement was to create a restful service to inscribe the students in SAP HCM using bapi BAPI_BOOK_ATTENDANCE this restful service is called from the e-learning system, to achieve this I do the following steps:

  1. Create handler class inheriting from CL_REST_HTTP_HANDLER class.


There we have to redefine the GET_ROOT_HANDLER method to make our own implementation.

In code of this method we especify the class that is in charge of implementing the service that we require to inscribe students, the class is ZCL_CREA_INSCRIPCION_RESOURCE.

The code snippet “/url_dest({file})” means that the url must end after domain name with this structure: example, if this is not that way, simply it will not be used the resource class ZCL_CREA_INSCRIPCION_RESOURCE, and you will be rejected with a message stating that the service is not implemented.

Why I use this url structure? well we agree with e-learning system provider that he is going to send me a file name that is dinamically generated each time they call my service, this file contains the students to be inscribed separated with commas, the comma separate file is in remote system, later I will give you more details.

    2. Create the resource that contains the code with response to the GET method call (ZCL_CREA_INSCRIPCION_RESOURCE).

This class has to inherit from class CL_REST_RESOURCE.

The class method to implement is GET, this is done through redefinition button.

In get method i’m going to explain the code in detail, so as I said, this second part is to respond to a request from external elearning system.

Let’s declare variables:

As part of the query parameters, i get the following, the file name comes as part of uri attribute (extracted from url_dest(file1.csv), remember example

The l_file variable would take the value file1.csv for the example given. About the query parameters Signature, Expires and AWSAccessKeyId they come as part of the same URL after “?” character, for example:

So after we read the uri attribute and the query parameters we are ready to respond the GET call, and after that (until now we are still in GET method) we have to download the file that external system tell us that is available to download, this is the csv file called file1.csv with the list of the students that SAP has to inscribe. So still in GET method of class ZCL_CREA_INSCRIPCION_RESOURCE we read the url of the external url we’re going to connect to download the file, this is done using class cl_http_client method create_by_destination, I choose this method because is easy to use, but if you want you could use create_by_url, using this method we have to create a RFC destination. After define the url, we set the HTTP method that in this case will be GET method, this is done calling the set_method from the request object from the lo_http_client.

In the rfc destination we specify only the target host that we are going to connect.

Returning to the class method GET,  we will construct the uri from the url that we will pass to the external system, this uri parameters were the uri parameters sent by external system when they invoke sap url (note that this ulr’s are only for illustration, so don’t try to open it)  and this values are the data we will use to validate ourselves to the server where the file is. Using class cl_http_utility method set_request_uri we set the uri parameters, so after that only connect, send and receive the data.

Once we receive the file from external server we get it from response object with method get_cdata, and convert the csv file to an internal table.

If the file is empty we return a json structure issuing an error message, if the file is not empty so the response will be positive using the same json structure with “ok” message, this ok message means only that the file was received successfully, but not that the students were inscribed, the inscription process is made by function module ZHCMFM_INSCRIP_PROC_RESP as asynchronous process (STARTING NEW TASK syntax), we describe this function module fully in next part.

    3. Function module ZHCMFM_INSCRIP_PROC_RESP.

This function module will inscribe the students in HCM events management module, for this process to complete the function receives an internal table with the personnel number and event code, with this within a loop we inscribe the students using bapi BAPI_BOOK_ATTENDANCE and save log in another internal table.

In the next section I pass the data from log internal table l_it_book_attend_err to l_it_inscript internal table.

At the end I take the internal table l_it_inscript and convert it to a json object using call transformation, I invoke the url where is listening for result of the student’s inscription process and send the json object with set_binary_data method. After that I read the response that is an json object too with the result of the calling with two fields: status and error.

That’s all for this time, I hope you enjoy this blog and if you have any question don’t hesitate to ask.

Jhon Jairo Teran.