Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
vishaldubey
Advisor
Advisor
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!

 
7 Comments
0 Kudos
Hi Vishal,

 

while invking any webservice (even simple as in the example above) am getting timeout issue. can you please help on this.

 

Divya BH

Divya.BH@Cognizant.com

 
vishaldubey
Advisor
Advisor
0 Kudos
Hi divya_bh68

I would suggest you that first try a web service call in Postman. If the timeout issue is coming then the same will be replicable from Postman and once it is resolved then it will start working in Desktop Studio/Agent.

Regards,

Vishal Dubey
0 Kudos

Hi Vishal,

Thanks for your quick reply. “https://www.sap.com/index.html” this simple URL is working fine in postman and getting response. (i have attached screenshot for both Postman response and Webservice call from Desktop Studio). can you please check is there any format i need to follow while webservice invoke.

Postman response for same example web service

WebService calling from Desktop Studio

 

Regards

Divya.BH@Cognizant.com

vishaldubey
Advisor
Advisor
0 Kudos

Hi divya_bh68

If you are trying to download a remote file then please have a look at the below-attached code. It requires LocalFile path as well which will not be auto-generated.

Regards,

Vishal Dubey

// ----------------------------------------------------------------
// 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);
}
});
}});
former_member720518
Discoverer
0 Kudos

Hello,

thank you for your post. It is very useful.

I want to use SAP document Extraction Service

But I get an error ctx.ajax.call error: Not Found'

I tried postman and get a response successfully but SAP IRPA desktop get an error

Can you help me


postman

 

RPA

sveabecker
Community Advocate
Community Advocate
0 Kudos
Hi zahraa_samir I would recommend to ask your question in the community https://answers.sap.com/index.html instead of here so that you can reach a broader range of experts. Make sure you select the correct tags, too. This tutorial might help you to get more familiar with asking questions in SAP Community: how to ask and answer questions in the community

Furthermore, if you're hoping to connect with readers, please consider adding a picture to your profile. Here's how you do it: https://www.youtube.com/watch?v=F5JdUbyjfMA&list=PLpQebylHrdh5s3gwy-h6RtymfDpoz3vDS. By personalizing your profile with a photo of you, you encourage readers to respond.

Regards,

Svea

SAP Community Global Moderator
0 Kudos
Hello dears,

I have implemented the code in DesktopStudio, and it throws me a timeout, since the service works OK by Postman, any suggestions?