Skip to Content
Technical Articles

SAP Intelligent RPA – Simplified API / Web Service Call

Introduction

In this blog post, you will learn how API or Web Service call works as well as how to download a remote file. SAP Intelligent RPA provides methods and activities to call an API or Web service. The APIs and metadata make bots more robust.

Purpose

This approach gives the flexibility to integrate your bots with legacy or web apps or backend applications. If you want to build automated solutions with integrations, and extensions easily, you are at the right place to get started. You can do this by consuming API and Web Service calls.

Product

SAP Intelligent RPA version used: Desktop Studio 2.0.0.173

How it works

The below scenario is well suited when there is a requirement to integrate multiple applications. Too many useless screens, containing useless data, but never mind. Robots are here to clean up the place, only picking up the mandatory data using web service calls, and let the user do his job with more accuracy.

Here, web service calls are implemented by ctx.ajax.call method. Let’s check the details below.

SAP Intelligent RPA Web service call:

  • Supports REST Get, Post, Put, and Del requests
  • Supports SOAP API call
  • By default, a Web service call is driven in an asynchronous style
  • Dedicated activity to call SAP Web service and other Web services
  • Desktop SDK has ctx.ajax class to provide ‘HTTP request’ management.

Activity Parameter:

  • Source URL: URL to be called
  • Input data: Optional data
  • Output data: The callback function in “Success” called when the call is successfully terminated and stores the response in the mentioned data item.
  • Data content type: Type of data you are sending like XML, JSON,  binary, etc.
  • Web service call method:  Method type (del, get, put, post,  options, head, etc.)
  • Supported Data Content-Type:
  • Supported Web Service Call Methods:
  • Generated Code Snippet:
    • Respective code generated depending on the parameter value after build
    • You can change the generated code as needed
    • By default, the ctx.ajax.call() is an asynchronous call
    • To make a synchronous call, set the “async” parameter to “false”
    • If the Web service call is successful, then the function for success parameter will be executed; if not, the function for the error parameter will be executed

API / AJAX / Web Service call examples:

Example 1: In this example, I am using the OData API from SAP API Business Hub. Please note that to call below API service, the API Key is mandatory.

  • Tryout to check it is working or not – for me it is working fine.
  • Before implementing it in the SAP Intelligent RPA Desktop Studio, it is recommended to try out the same in Postman – if it is working fine then go-ahead to implement it in your automation solution.
  • As it is working fine here hence now it is time to implement it in Desktop Studio. Here, I am calling OData API and storing the response to MS excel file.
// ----------------------------------------------------------------
//   Step: Call_SAP_web_service
// ----------------------------------------------------------------
GLOBAL.step( {
	Call_SAP_web_service: function (ev, sc, st) {
		var rootData = sc.data;
		ctx.workflow('webserviceCall', 'a4214b71-7233-40c1-a342-29b99997ec57');
		// Call a web service.
		ctx.ajax.call( {
			url: 'https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/A_BusinessPartner?%24top=50&$top=10',
			method: e.ajax.method.get,
			data: rootData.inputData,
			contentType: e.ajax.content.json,
			//	  usePassport: true,
			async: false,
			header: {
				"APIKey": "xxxxxxxxx",
				"Accept": "application/json"
			},
			success: function (res, status, xhr) {
				rootData.APIResult = res.d.results;

				ctx.log("Success");
			},
			error: function (xhr, error, statusText) {
				ctx.log(' ctx.ajax.call  error: ' + statusText);
			}
		});

		sc.endStep(); // Create_Excel_file_
		return ;
	}
});

Example 2: In this example, I am using the OData API call with basic authentication using user id and password.

  • Design workflow as below
  • Generated code snippets with the addition of basic authentication.
// ----------------------------------------------------------------
//   Step: Call_a_web_service_1
// ----------------------------------------------------------------
GLOBAL.step( {
	Call_a_web_service_1: function (ev, sc, st) {
		var rootData = sc.data;
		ctx.workflow('webserviceCall_BasicAuth', '66bd6f33-ca62-4310-97f5-ac886a431bab');
		// Calls a web service.
		var userid = "xxxxxxx"; // Change this
		var pwd = "xxxxxxx"; // Change this
		// Convert it to base64 encoding
		var auth = ctx.base64.encode(user + ":" + pwd);

		ctx.ajax.call( {
			url: "https://xxxxxxxxx.successfactors.eu/odata/v2/User?$top=2",
			method: e.ajax.method.get,
			data: rootData.excelData,
			contentType: e.ajax.content.json,
			header: {
				"Authorization" : "Basic " + auth
			},
			success: function (res, status, xhr) {
				//$data$ = res;
				var apiResponse = res.d.results;
				sc.endStep(); // end Scenario
				return ;
			},
			error: function (xhr, error, statusText) {
				ctx.log(' ctx.ajax.call  error: ' + statusText);
			}
		});
	}
});

Remote File Download: Download file using ctx.ajax.call()

Parameter:

  • url: URL of the file
  • localFile: Destination path of the file including filename
  • Backslash “\” is a reserved keyword in JavaScript so use escape character and replace single backslash with “\\” to make the desired file path

Example: In the below example, the website page is being downloaded and stored at the specified location. Here, a Web Service activity “Call web service” is used, and below are the generated code snippet.

  • Workflow design using Web Service activity “Call web service”
  • Generated code snippet:
// ----------------------------------------------------------------
//   Step: Call_a_web_service
// ----------------------------------------------------------------
GLOBAL.step({ Call_a_web_service: function(ev, sc, st) {
	var rootData = sc.data;
	ctx.workflow('remoteFileDownload', 'efc17622-fe58-462a-a0bb-3e667e230ccf') ;
	// Calls a web service.
	ctx.ajax.call({
	  url: "https://www.sap.com/index.html", // URL of the file to be downloaded
	  localFile: "C:\\SAP_iRPA\\SAPHomeIndex.html", //Destination of the file including filename
	  method: e.ajax.method.get,
	  data: rootData.inputData,
	  contentType: e.ajax.content.json,
	  success: function(res, status, xhr) {
//	    $data$ = res; // Keep your variable to store the API response/result data
			ctx.log("Success");
		sc.endStep(); // end Scenario
		return;
	  },
	  error: function(xhr, error, statusText) {
	    ctx.log(' ctx.ajax.call  error: ' + statusText);
	  }
	});
}});

Conclusion

Now you should be able to call API / OData / Web Services in SAP Intelligent RPA automation solution and thus make your SAP iRPA bots more robust.

Happy Learning!

 

Be the first to leave a comment
You must be Logged on to comment or reply to a post.