Technical Articles
SAP Intelligent Robotic Process Automation: Updating an Existing Sales Contract’s Entity
SAP’s Intelligent Robotic Process Automation is naturally used for automating repetitive processes to reduce the number of tedious tasks as well as speeding up deliveries. These benefits allow users focus on more important tasks and enhance the speed of decision making. Within SAP’s Intelligent Robotic Process Automation, it encapsulates HTTP Request management which allow users to automate web service calls.
In this blog post, we will explore to how to do a PATCH method by calling a web service in SAP Intelligent RPA. For this scenario, the user is taking an endpoint for an existing sales contract management and updating the specific entity, PurchaseOrderByCustomer.
Steps to do a PATCH request to Update an Entity:
- Setup environmental variables.
- Creating a GET request for required Headers.
- Sending a PATCH Request to update an entity.
Prerequisites:
- Desktop Studio & Desktop Agent installed
- An API URL (In this case, I’ll be using a sales contract API URL)
- Knowledge on fetching variables from SAP Intelligent RPA Cloud Factory
Step 1. Setup environmental variables:
- Create a custom activity, “GetEnvVar”, where you will reference your API’s endpoint and credential.
Custom Code for GetEnvVar Activity:
GLOBAL.step({ GetEnvVar: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('SinglePatchWorkflow', 'd37c19a1-2bdd-4b6a-a9d9-f69e16d1aecd') ;
// GetEnvVar
var settingDeclaration = {};
settingDeclaration["salesUrl"] =
{
key: ctx.cryptography.keys.none,
server: true
}
ctx.setting(settingDeclaration);
/*Declaring Setting for Credential*/
var credDeclaration = {};
credDeclaration["salesAcc"] =
{
server: true
}
ctx.cryptography.credential(credDeclaration);
//Endpoint Url
ctx.settings["salesUrl"].get(function (code, label, setting) {
if (code === e.error.OK) {
sc.data.endPointUrl = setting.value;
} else {
sc.endScenario();
return;
}
});
//Credentials
ctx.cryptography.credentials["salesAcc"].get(function (code, label, credential) {
if (code === e.error.OK) {
sc.localData.cred =
{
username : credential.userName.get(),
password : credential.password.get()
}
sc.data.bAuth = ctx.base64.encode(sc.localData.cred.username+":"+sc.localData.cred.password);
} else {
sc.endScenario();
return;
}
});
sc.endStep(); // getToken
return;
}});
Step 2. Creating a GET request for required Headers:
- Create a “call web service” activity to do a GET request in order to fetch the necessary information to do a PATCH request for your endpoint. In this case, I had to modify the script to grab the Etag, x-csrf-token, and set-cookie after allowing the workflow to build. Learn more on how to get CSRF Token.
- NOTE: A custom function was required for my specific endpoint to parse the response headers since my endpoint is a HTTPS If your endpoint is a HTTP request, you may resort to xhr.getResponseHeader(‘’) instead of parsing headers manually with xhr.getAllResponseHeaders() as I did.
Modified Activity Code for getToken:
GLOBAL.step({ getToken: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('SinglePatchWorkflow', 'b5425172-b64f-43f4-a5db-226e09bc37a8') ;
// getToken
ctx.ajax.call({
url: sc.data.endPointUrl,
method: e.ajax.method.get,
contentType: e.ajax.content.json,
async: false,
usePassport: true,
ignoreClientCertificate: true,
headers: {
Authorization : "Basic "+sc.data.bAuth,
'X-CSRF-Token' : 'Fetch'
},
success: function(res, status, xhr) {
var allHeaders = xhr.getAllResponseHeaders();
sc.data.etag = getHeader("ETag", allHeaders);
sc.data.token = getHeader("x-csrf-token", allHeaders);
sc.data.cookie = getHeader("Set-Cookie", allHeaders);
},
error: function (res) {
ctx.log(' fail get token error: ' + res);
}
});
sc.endStep(); // PatchRequest
return;
}});
Step 3. Sending a PATCH request to update an entity:
- Create a “call web service” activity to do a PATCH request and reference the information that was obtained from your GET request.
- NOTE: There is no PATCH option available, so select any web service call method and allow the workflow to build then go within the code and change the method to “PATCH”.
Modified Activity Code for PatchRequest:
GLOBAL.step({ singlePatch: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('SinglePatchWorkflow', 'b3f546df-0ade-4c7c-9763-84202e5a61a7') ;
// PatchRequest
var payloadData = JSON.stringify({"PurchaseOrderByCustomer":"patchTest - 08"});
ctx.ajax.call({
url: sc.data.endPointUrl,
method: 'PATCH',
data: payloadData,
contentType: e.ajax.content.json,
usePassport: true,
ignoreClientCertificate: true,
headers: {
"X-CSRF-TOKEN": sc.data.token,
"Authorization": "Basic " + "Basic "+sc.data.bAuth,
"Cookie": sc.data.cookie,
"If-Match": sc.data.etag
},
success: function(res, status, xhr) {
ctx.log("success");
},
error: function (res) {
ctx.log('fail: ' + res);
}
});
sc.endStep(); // end Scenario
return;
}});
- The entity that will be updated is PurchaseOrderByCustomer with patchTest – 08.
On a final note, you will be able to do a PATCH request by following the steps in this blog post. Learning how to do a PATCH request will allow users to extend their bots to make it even more autonomous and efficient. Users can even incorporate loops with the PATCH request to update multiple entities as well as different endpoints. I hope this blog post will offer guidance to anyone that may be implementing API calls in their iRPA bots.
Thank you for taking the time to read this blog post! If you have any questions or comments feel free to reach out in comments or by my e-mail. In the future, I’ll incorporate more API tutorials such as: PATCH Request via XMLHttpRequest and looping with ajax calls.
Below are several Helpful Links that can offer guidance:
- Learn more about AJAX calls with SAP Intelligent Robotic Process Automation.
- SAP Intelligent Robotic Process Automation Community Forum Question Page
- SAP Intelligent Robotic Process Automation Community Blogs Page
Helpful Tutorials From Intelligent Automation Task Force:
- Merging Two SAP Intelligent RPA Projects Without Applications
- SAP Gateway Tracing and Error Checking
- Activate Available OData in SAP Gateway
- Get Full URL & Sample Payload from SAP Gateway