Skip to Content
Author's profile photo Jhon Jairo Teran Salazar

Simple integration with third party e-learning system using restful and json in ABAP

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:

Imagen 1.png

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

Imagen 1.png

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

Imagen 1.png

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.

Imagen 1.png

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.

       Imagen 1.png

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

Imagen 1.png

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.

Imagen 1.png

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.

Imagen 1.png

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

Imagen 1.png

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:

Imagen 1.png

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:

Imagen 1.png

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.

Imagen 1.png

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

Imagen 1.png

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.

Imagen 1.png

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.

Imagen 1.png

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.

Imagen 1.png

Imagen 1.png

    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.

Imagen 1.png

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

Imagen 1.png

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.

Imagen 1.png

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.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Ragavendiran Janakarajan
      Ragavendiran Janakarajan

      Hi Jhon,


      Thank you for the wonderful blog explaining the integration of abap with json and vice versa,

      I have followed the steps as described above but i am facing an issue when i am trying to receive response from third party system through rest based json service,

      The issue is that, while using mo_rest_client->post(lo_rest_entity), The method post is set, then the I am getting in entity as cl_HTTP_request.

      while using send, there is no issue and data is sent but while receiving the data, I am facing exception as HTTP_communication_failure and HTTP_invalid_state.

      Could you please let me know, how can i resolve the issue.

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Hello  Ragavendiran, thanks for reading my blog.

      Could you please paste the code you are talking about and specifically where raises the exception HTTP_communication_failure and HTTP_invalid_state, and what is the http status code that returns the service.

      I want to understand a little bit more the issue.

      Best regards.

      Jhon Jairo.

      Author's profile photo Ragavendiran Janakarajan
      Ragavendiran Janakarajan

      Thanks Jhon,

      It was due to the incorrect url and now it is resolved.

      I am facing an issue for conversation of abap data to json format, I have converted 5 xstrings using call transformation method as mentioned above.

      I converted all of them to strings and concatenated all the strings and converted the result string to a result xstring using FM ECATT_CONV_STRING_TO_XSTRING.

      When i click on the result xstring, The xml browser was not displaying with xstring length as 659, hence i used

      data: lv_json_res type xstring

      lv_json_res = lo_json_writer->get_output( ).

      and the lv_json_res was picking the last string alone with 93 as length only.

      I was not able to concatenate multiple strings, when i did it, It was the picking the first string only

      concatenate xstring1 xstring2 xstring3 xstring4 to json_result in BYTE MODE,

      please let me know how we can resolve this issue.