Let’s imagine a typical use case – your automation should fetch multiple invoice files from some source (file folder, Outlook, Gmail, FTP, etc.). Automation will loop through your file collection, extract data from those invoices using DOX and then analyze if everything is OK. Automation can post some of the invoices but what if PO is missing, or your 3-factor authentication found problems, or confidence level of recognition is low for some elements, or for larger total amount you must have a multi step approval?

There can be many different workflows that you will need to fire based on different conditions found in your invoices.

This blog post will show you how to do that – call SAP Build Process Automation [SBPA] Business Process  (workflow artifact) from an Automation (RPA artifact).

 

Figure 1: Process example (click to enlarge)

This was just one use case example but there are many situations and more complex scenarios where you will need to automate something and based on detected conditions trigger a different workflow for each condition.

Figure 2: Automation example (click to enlarge)

Assumption is that all your processes are created and deployed in SAP Build Process Automation and each process is triggered by API call. (To find out how to create a process workflow with API trigger check this blog.)

Figure 3: Process’ API trigger example (click to enlarge)

 

In most cases your workflow will be part of the same project as your automation, but please note that you can call any SBPA workflow that is part of another project or even deployed on another tenant (as long as you have needed security credentials).

 

Solution

In monitoring part of the SBPA check details of your deployed process API. It’s important to know the expected JSON payload structure for this particular process (red rectangle). It’s important to know exact field names as well as type of values (in this example 2 string inputs, one numeric and one boolean). On this screen you will find URL and definitionID values that we will need later (yellow rectangle).

In your project create a new custom data type called “Context”. It’s important that names and types are exactly the same as in your API definition.

 

Now create an additional  custom data type called “WF API Trigger” that will hold all necessary (project/tenant specific) data to execute an API call.

 

Define 2 new variables in your automaton project. First is of type “WF API Trigger” and rename output to myAPI.

For input values select custom data and add needed values. Before you saw where to find values for api_url and definition_id. To find other values you will need to go to your BTP Cockpit and click on service key of your SBPA instance and copy values from pop-up window:

Values for client_idclient_secret and token_url can be found here:

Please note that url from service key MUST be appended with “/oauth/token” so the value of token_url should look like this “https://yourtenant.authentication.eu10.hana.ondemand.com/oauth/token“.

Now add any values you want for Context variable and rename it to myContext:

Before calling the web service, you must prepare the JSON object with all the necessary data. There is a package in the Store called “Web services Best Practices”. I encourage you to check those examples (as well as other packages from the store because they are great for learning and inspiration).

However, examples in this package don’t cover cases where you need to authenticate using oAuth 2 (we first need to fetch the token and then execute POST call with payload)  so you will now add custom script to your project and define 2 inputs (myAPI of custom data type WF API Trigger and myContext of type Any) and output named options of type Any.

Click on “edit script” to add code and define inputs and output.

Copy-paste this code:

async function fetchToken() {
    
    const data_cred="client_id="+myAPI.oAuth.client_id+
    "&grant_type=client_credentials"+
    "&client_secret="+myAPI.oAuth.client_secret;
    
    const options = {          
        method: 'POST',
        url: myAPI.oAuth.token_url,
        body:data_cred,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        resolveBodyOnly:true
    };
    try {
        const response = await irpa_core.request.call(options);
        const token = JSON.parse(response);
        var at=token.access_token;
        return at;   
    } catch (error) {
        irpa_core.core.log(error);
    }
}

let token = await fetchToken();

// ------------------------------

let input = {
    "definitionId":myAPI.definition_id,
    "context":myContext
};

let auth = "Bearer " + token;

let output = {
    method: 'POST',
    url: myAPI.api_url,
    headers: {
      'Content-Type': 'application/json',
      'Authorization':auth
    },
    responseType: 'json', // parse the body of the result to get a JSON object
    resolveBodyOnly: true, // get only the body of the response
    json: input
};

return output;

Now you just need to add the standard action “Call Web Service” and use the output JSON object from the Custom Script as the input for this step. Also define name for output object (in this example “api_output”):

Optionally add “Log message” action to show JSON object api_output in console and execute your automation.

Upon successful execution you can find that JSON response object contains (among other data) ID of your started process:

 

If you go to the monitoring section of SAP Build Process Automation you can find that your process is running:

 

Next steps

In my next blog post I will show you how to easily turn this project into a generic solution that you can just add in all of your projects where you need to call workflow API from an automation.

To stay current will all the new and exciting things related to SBPA please check:

Enjoy building with SAP Build. 🙂

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x