Wednesday 7 April 2021

How to use a REST API to post data into SAP S/4HANA Cloud using SAP RPA 2.0

Introduction

With the desktop studio application, developers are free to create custom RPA bots which simplifies days to day tasks by reducing human intervention. With RPA 2.0 and its Low-Code approach, I will show you how to leverage the SAP Intelligent RPA Cloud Studio to create the API call.

I will be using the supplier invoice creation API which can be accessed via activating the Communication Arrangement SAP_COM_0057 in the SAP S/4HANA Cloud system.

Create Automation

The first step would be to login to the SAP RPA Cloud factory, go to the projects tab and create a new project.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Enter the bot name and click on create.

This will create a new project and will open the project in a new window.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Under the contents, tab, Click on Create and then select Automation

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Provide the necessary details and click on create

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

This will create a new automation with an empty workflow as shown

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Let us create two input variables for providing username and password of type String. This can be done by going to the I/O tab and adding two input parameters as shown

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

The next step will be to encode the username and password. The encoded string can be used to make the REST API calls.

Search for Encode String under the tools tab to get the Encode String activity.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Drag and drop this activity to the RPA workflow as shown below

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

You will now see that the activity has a red highlight. This is because it expects an input to be encoded.

Click on the activity Encode String. This will open the input fields for the activity. Select the highlighted button next to the input

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

This will open the Expression Editor.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Expand the Tree variables and click on both username and password and maintain as shown. Click on test expression to validate the input

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

The encrypted output will be returned via the Output Parameter, result of type String, which will be created automatically.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

I have renamed this to credentials. You can do this by clicking on the Output Parameters field directly.

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

A custom script step should be added to fetch the CSRF token for posting the API.

Add the step custom script to the workflow from the tab Tools and rename it to Fetch Token

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Double click on the activity Custom Script and add the field credentials as an input of type String and paste the following code in the script

async function fetchToken() {
    const options = {          
        resolveBodyOnly : false,
        method: 'GET',
        url: 'https://xxxxxx-api.s4hana.ondemand.com/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_SupplierInvoice',
        headers: {
            'Accept' : 'application/json',
            'Content-Type': 'application/json',
            'Authorization' : 'Basic ' + credentials,
            'x-csrf-token': 'fetch'
        }
    };
    try {
        const response = await irpa_core.request.call(options);
        return response;   
    } catch (error) {
        const csrfToken = error.response.headers['x-csrf-token'];
        return error;
    }
}
let response = await fetchToken();
return response.headers;

Create an output parameter and name it as options, to return the output data and maintain the type as Any

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

You would still see the red highlight on the Activity Fetch Token. This is because the input field credentials is not yet mapped. To solve this, single click on the Activity Fetch token and map the output field credentials of the previous step to this

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

This step will return the required tokens and cookies.

Add another Custom step to make the API post call and rename the step to Post API. We will build the test payload also in this step.

Add the input parameters csrf_token, cookie and credentials of type String and an output parameter payload of type Any and paste the following code in the script

var i = 0; 
var temp ="";
var cookieField ="";
if(cookie!=""){
    for(i=0;i<cookie.length-1;i++)
    {
        temp = cookie[i].split(";");
        cookieField = cookieField + temp[0] + "; ";
    }
    temp = cookie[i].split(";");
    cookieField = cookieField + temp[0];
}
var data = {
    "FiscalYear": '2020',
    'CompanyCode': '1710',
    'DocumentDate': '2020-02-20T00:00',
    'PostingDate': '2020-02-20T00:00',
    'InvoicingParty': '17300001',
    'DocumentCurrency': 'USD',
    'InvoiceGrossAmount': '520.00',
    'AccountingDocumentType': 'RA',
    'TaxIsCalculatedAutomatically': false,
    'to_SuplrInvcItemPurOrdRef': [
        {
            'FiscalYear': '2020',
            'SupplierInvoiceItem': '1',
            'PurchaseOrder': '4500000000',
            'PurchaseOrderItem': '10',
            'Plant': '',
            'ReferenceDocument': '',
            'ReferenceDocumentFiscalYear': '',
            'ReferenceDocumentItem': '',
            'TaxCode': 'I1',
            'DocumentCurrency': 'USD',
            'SupplierInvoiceItemAmount': '520',
            'PurchaseOrderQuantityUnit': 'PC',
            'QuantityInPurchaseOrderUnit': '10',
            'QtyInPurchaseOrderPriceUnit': '1',
            'to_SupplierInvoiceItmAcctAssgmt': {
            'results': []
            }
        }
    ]
};
var payload = {
    resolveBodyOnly : true,
    method: 'POST',
    url: 'https:// xxxxxx-api.s4hana.ondemand.com/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_SupplierInvoice',
    headers: {
        'Authorization': 'Basic '+ credentials,
        'Cookie' : cookieField,      
        'Content-Type': 'application/json',
        'x-csrf-token' : csrf_token
    },
    ignoreClientCertificate: true,
    body: JSON.stringify(data)
};
return payload;

Make sure to modify the input payload according to your system landscape settings.

Now, there will be a red highlight on the Activity Post API. Maintain the input for csrf_token, cookie and credentials as follows

csrf_token

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

cookie

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

credential

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Save the expressions

Add the step Call Web Service from the tools tab and maintain the input as follows

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Add the step Log Message to log the output of the web service call and maintain the input as shown

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

Save the workflow and click on test.

Provide the username and password as input and click on test

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

This will post the supplier invoice to the SAP S/4HANA Cloud system and return the Supplier invoice number as shown

SAP S/4HANA Cloud, SAP HANA Exam Prep, SAP HANA Certification, SAP HANA Prep, SAP HANA Career, SAP HANA Preparation

You can covert this to JSON by adding a header type ‘Accept’: application/json in the Custom Script Post API and continue to use this in your application as required.

No comments:

Post a Comment