Skip to Content
Technical Articles

Calling a Web Service

This is a submission of the Enhance your bot building with templates blog post series.

Introduction

In this post I will show you how to access a web service via API through SAP Intelligent Robotic Process Automation Desktop Studio. This will be shown by accessing an SAP OCR API. The newest release of Desktop Studio (1.0.9) has built in functionality to extract text from a readable PDF and thus accessing this specific API may not be necessary. However, it is important to understand how to access web services in general in Desktop Studio for many projects.

What will we learn in this tutorial

  • How to access an API using an AJAX call
  • How to call the specific SAP OCR API
  • How to parse a text file with custom code

Steps to follow

  1. Create a project and workflow in Desktop Studio
  2. Add activities to the workflow
  3. Edit the activities in editor to call the OCR service API
  4. Parse the results of the API call

Prerequisites

  • Desktop Studio and Agent (1.0.8), connected to Factory tenant

Instructions

1. Create a project and workflow in Desktop Studio

  1. Create a new project and give it a name.
  2. Go to ‘Scripts’ tab.
  3. Right click on ‘Global’, click ‘Include library Script’, then enable ‘String Utilities’ (under “Utils” folder) and click “Save’.
  4. Go to the ‘Workflow’ tab and create a new workflow. Name it whatever you would like to.

2. Add activities to the workflow

  1. Drag and drop the following activities into the scenario. “Write text file” and “Extract Specific Text” activities are custom activities.
  2. Click on the “Call a web service” activity and fill out the information under properties as it pertains to your particular API.This information can be found on the webpage description for the API. For example:

3. Edit the activities in editor to call the OCR service API

  1. Build the project and navigate to the “Scripts” perspective.
  2. The “Call a Web Service” function will need to be edited for your specific API. For example, the necessary code to call the OCR API is:
    // ----------------------------------------------------------------
    //   Step: Call_SAP_OCR_API
    // ----------------------------------------------------------------
    GLOBAL.step({ Call_SAP_OCR_API: function(ev, sc, st) {
    	var rootData = sc.data;
    	ctx.workflow('ExtractPDFText', '65e46980-804c-4875-a560-865f25925854') ;
    	// Call SAP OCR API
    	ctx.ajax.call({
    		url: rootData.APIUrl,
    		formData: {
    			file: rootData.PDFFileLocation,
    			type: e.ajax.content.pdf,
    			name: 'files'
    		},
    		header: {
    			Accept: e.ajax.content.json,
    			APIKey: rootData.APIKey
    		},
    		method: e.ajax.method.post,
    		success: function(res, status, xhr) {
    			rootData.PDFContent = res.predictions[0];
    			sc.endStep(); // end Scenario
    			return;
    		},
    		error: function(res) {
    			ctx.log(' ctx.ajax.call  error: ' + res);
    		}
    	});
    }});​

    The following definitions may be helpful:

    • url: URL needed to access API
    • formData and header: necessary information for the API to work. The format of these will be described in the API information page. For example, “file” is the location of the PDF file.
    • APIKey: your specific API key that allows you to access the API (if necessary)
    • Method: method used to access API. Here we use a “Post” method.
    • Success: What the bot should do on success of the API call. Results from the call will be saved to rootData.PDFContent.
    • Error: what the bot should do on an error produced from the API call.

4. Parse the results of the API call

  1. Write the results in rootData.PDFContent to a text file as below (“rootData.textFileLocation” represents the location on your computer where you want to save the results to):
    // ----------------------------------------------------------------
    //   Step: Write_a_text_file
    // ----------------------------------------------------------------
    GLOBAL.step({ Write_a_text_file: function(ev, sc, st) {
    	var rootData = sc.data;
    	ctx.workflow('ExtractPDFText', 'a6850628-1e6f-452b-a9a6-a573c30063a3') ;
    	// Write a text file
    	ctx.fso.file.create(rootData.textFileLocation);
    	ctx.fso.file.write(rootData.textFileLocation, rootData.PDFContent, e.file.encoding.UTF16);
    	sc.endStep(); // Extract_Specific_Text
    	return;
    }});​
  2. Parse the text file as below:
    // ----------------------------------------------------------------
    //   Step: Extract_Specific_Text
    // ----------------------------------------------------------------
    GLOBAL.step({ Extract_Specific_Text: function(ev, sc, st) {
    	var rootData = sc.data;
    	ctx.workflow('ExtractPDFText', '6cb23853-573f-4d96-a287-2ddb9ac7d3d5') ;
    	// Extract Specific Text
    	ctx.string.init();
    	rootData.date = rootData.PDFContent.split("DATE: ")[1].split("\n")[0];
    	rootData.invoiceNum = rootData.PDFContent.split("INVOICE #: ")[1].split("\n")[0];
    	rootData.totalDue = rootData.PDFContent.split("TOTAL DUE")[1].split("\n")[0];
    	sc.endStep(); // Write_to_Excel
    	return;
    }});​

    Here, the text file is parsed to find the Date, Invoice #, and Total Amount Due. These are found by extracting the text following these markers. This type of text extraction is custom for every PDF document, and thus the PDF document must be examined and a strategy to find the information must be decided on prior to writing the code above. The code shown is only an example of how to extract important information for one specific PDF format.

Conclusion

This blog post should help you to understand how to call an API web service using SAP Intelligent Robotic Process Automation. It does this by calling the SAP OCR API and extracting necessary information from the result. You should now understand how to call web services on the Desktop Studio.

1 Comment
You must be Logged on to comment or reply to a post.
  • hi Steven,

    do you have any sample for this blog? i changed above parameters but executed failed, log shows :undefined error, can you pls help me to correct it? thanks