Skip to Content
Technical Articles
Author's profile photo Thomas Jentsch

SAP RPA 2.0: Custom script SDK for SAP Analytics Cloud API – sample

This is the part of the tutorial, where we will develop a Custom script SDK for the SAP Analytics Cloud REST API using the Design Pattern.

Tutorial Blogs

  • Overview and Concept (please read this first)
    Getting started
  • Custom script SDK – Design Pattern (Package is used in this tutorial)
    Overview, Concepts and Step by Step guide
  • Custom script SDK – Rest API sample (this tutorial)
    Step by Step guide using the Rest API of SAP Analytics Cloud
    provides the template for a JavaScript class using the HTTP request activity from the SAP Intelligent RPA core SDK

The tutorial can be used also as template to implement Custom script SDKs for other APIs.
Here an overview for the design pattern approach with package dependencies:

Overview

  • Create a new project
  • Develop the content for samples Activities to automate SAP Analytics Cloud (SAC) tasks: Logon, Add Team and Logoff
    The JavaScript code using the SAC Rest API can be copied from this blog.
    We are using the HTTP request activity from the SAP Intelligent RPA core SDK for the rest calls.
  • Create an automation to test the SAC activity Logon
  • Add and test the SAC activity Add Team
    show how parameters can be passed to an activity
  • Add and test a new Activity: Delete Team
    show how easy you can add an activity and extend the JavaScript code

We will not generate the Custom script SDK package for SAC, but feel free to do this in your SAP Intelligent RPA factory and test the package in your own project.

Here the final automation to test all the steps:

Create a New Project

  • Use Project name: Custom script SDK – SAP Analytics Cloud
    Description: Use the package – Custom script SDK Sample & Design Pattern

Add Package Dependency

  • Select Dependencies – Manage Dependencies
  • Select Other and click Add Dependency
  • Select the Package Custom script SDK Sample & Design Pattern and Version from the list
  • Use Alias csdk_core (the default name is to complex)
  • Select Add
  • The package is in the list, select Close

Create Data Types

Step: Create Data Type

  • Use the name cs_activities_SAC
  • Define the fields of the data type
    for the Enumeration, add the name of the activities: logon,addTeam,logoff
    (the allowed and implemented activities for your API)
  • Select Save

Step: Create Data Type

  • Use the name cs_config_SAC
  • Define the fields of the data type as show in the picture below
    – use New Field to add a new row
    – use New Child to group parameters, here name and secret of the oAuthClient
    – use field names: tokenURL, tenantScimURL, oAuthClient, name, secret

Automation – cs_package_SAC

Reuse the function ActivityCore(), define a custom script with the implementation of the SAP Analytics Cloud API in the JavaScript class SAP_AnalyticsCloud and save the activities definition in the package variable.
We will use the SAP Analytics Cloud Tenant API REST call for logon and to create a Group (we will use the term Team as used in the SAC Application).

Step: Create Automation

  • Use the name cs_package_SAC
  • Select I/O to define an Input and Output parameter
    The automation will take the SAP Analytics Cloud configuration as input and return the package definition, we will use in other automations later as input parameter
    Add Input Parameter: name configSAC, select type cs_config_SAC from the list
    Add Output Parameter: name: packageSAC, select type cs_package from the list
  • Save the automation

Step: Add Automation

  • Add the automation cs_function_Core (csdk_core) to your flow
  • Select the step and use name Define Function ActivitiesCore
  • Add the activity Custom Script to your automation
  • Select the activity Custom script in the flow diagram
  • Use Step name Define Activities Class
  • Select Edit Script
  • Add Input parameter: name fnActivityCore, select type Any
  • Add Input parameter: name configSAC, select type cs_config_SAC
  • Add Output parameter: name activities, select type cs_class
  • Copy the following script code to line 1 (the SAC API implementation)
    class SAP_AnalyticsCloud {
    
        constructor() {
            this._fnActivityCore = new fnActivityCore('SAP Analytics Cloud');
            this._accessToken = null;
            this._csrfToken = null;
        }
    
        async call(value, parameter) {
           await this._fnActivityCore.call(this, value.activity, parameter);
        }
    
        async logon() {
            this._fnActivityCore.log();
            this._csrfToken = null;
            const options = await this._requestOptions({          
                url: configSAC.tokenURL + '?grant_type=client_credentials',
                method: 'POST',
                useCsrfToken: false
            });
            const response = await irpa_core.request.call(options);
            this._accessToken = response.access_token;
            this._csrfToken = '';
        }
    
        async addTeam(parameter) {
            this._fnActivityCore.log();
            const options = await this._requestOptions({          
                url : configSAC.tenantScimURL + 'Groups',
                method : 'POST',
                useCsrfToken: true,
                body : JSON.stringify({
                    'id': parameter.name,
                    'displayName': parameter.name,
                    'members': [],
                    'roles': []
                })
            });
            const response = await irpa_core.request.call(options);
        }
    
        async logoff() {
            this._fnActivityCore.log();
        }
    
        // use get groups with a not existing group to get the x-csrf-token
        async _fetchToken() {
            this._csrfToken ='fetch';
            const options = await this._requestOptions({          
                url: configSAC.tenantScimURL + 'Groups/xy',
                method: 'GET',
                useCsrfToken: true
            });
            try {
                const response = await irpa_core.request.call(options);
            } catch (error) {
                return error.response.headers['x-csrf-token'];
            }
        }
    
        async _requestOptions(options) {
            const baseOptions =  {
                resolveBodyOnly : true, 
                responseType: 'json', 
                headers: {
                  'Accept' : 'application/json',
                  'Content-Type': 'application/json',
                  'Authorization': ''
                }
            };
            for (const x in options) {
                baseOptions[x] = options[x];
            }
            if (this._accessToken) {
                baseOptions.headers.Authorization = 'Bearer ' + this._accessToken;
                baseOptions.headers['x-sap-sac-custom-auth'] = 'True';
                if (options.useCsrfToken)  {
                    if (!(this._csrfToken)) {
                        this._csrfToken = await this._fetchToken();
                    }
                    baseOptions.headers['x-csrf-token'] = this._csrfToken;
                }
            } else {
                baseOptions.headers.Authorization = 'Basic ' + irpa_core.base64.encode(configSAC.oAuthClient.name + ':' + configSAC.oAuthClient.secret);
            }
            return baseOptions;
        }
    }
    
    return new SAP_AnalyticsCloud();
  • Close the Script View (X Icon on top right corner next to Save, or just click an empty area in the canvas)
  • Save the automation
  • The red marker is an indicator, that the parameters are missing
    Select the step in the flow
    Input parameters: fnActivityCore, select cs_function_ActivityCore from the list
    Input parameters: configSAC, select configSAC from the list
    Output Parameters should contain the name activities

Step: Create Variable

  • Add the activity to set the package parameters to your automation
    – select from Data – Data Types cs_package and drop to the flow diagram
  • Select the activity Create cs_package variable in the flow diagram
  • Use step name Create Package
  • Select Edit Activity
    parameter: instance select activities from the list
    Output Parameters: enter name cs_Package

Step: Set Automation Output Parameters

  • Select End in the flow diagram
    now we will assign the value for the output parameter of the automation, we have defined under I/O.
    Select cs_package from the list (output parameter from previous step)
  • Save your automation

Automation – cs_activity_SAC

Call an activity from the package, The Activity name and parameter values will be specified as input parameters when the automation is re-used in your automations (no-code).

Step: Create Automation

  • Use the name cs_activity_SAC
  • Select I/O to define the three Input parameters
    name: packageSAC, select type cs_package from the list
    name: activitySAC, select type cs_activities_SAC from the list
    name: parameter, select type Any from the list
  • Save your automation – select Save

Step: Add Custom Script

  • Select Tools and add the activity Custom Script to your automation
    Select the activity Custom script in the flow diagram
  • Use Step name Call Activity
  • Select Edit Script
  • Define three Input parameters, we will pass all automation parameters to the script
    name: activities, select type cs_class from the list
    name: activity, select type cs_activities_SAC from the list
    name: parameter, select type Any from the list
  • Copy the following script code to line 1
    await activities.instance.call(activity, parameter);
  • Close the Script View (X Icon on top right corner next to Save, or just click an empty area in the canvas)
  • Save your automation
  • The red marker is an indicator, that the parameters are missing
    select the step in the flow
  • For parameter activities, enter pack and select packageSAC.activities from the list
    (the list will not show the entry without typing ‘pack’)
  • For activity select activitySAC from the list
  • For parameter select parameter from the list
  • Save the automation

Automation – Test activity Logon

We will create an automation to test the logon to SAP Analytics Cloud.

Step: Create Automation

  • Use the name test
  • Add the activity to set the SAC configuration to your automation
    – select from Data – Data Types cs_config_SAC and drop to the flow diagram
  • Select the activity Create cs_config_SAC variable in the flow diagram
  • Use step name Define SAC configuration
  • Select Edit Activity
    enter the values as configured in your SAC tenant
    tokenURL: <your service>oauth2/api/v1/token
    tenantScimURL: <your tenant>/api/v1/scim/  (important: character / at the end)
    oAuthClient name and secret
    Output Parameters: enter configSAC
  • Add the automation cs_package_SAC to your flow
    Select the step and use name Define Package – SAP Analytics Cloud
    Input Parameter: configSAC, select configSAC from the list
    Output Parameters: should show packageSAC
  • Save the automation
    sometimes you have to close the automation and open again, cause parameter values will not be active for next steps
  • Add the automation cs_activity_SAC to your flow
    Select the step and use name Activity – logon
    Input Parameter: packageSAC, select packageSAC from the list
    Input Parameter: activitySAC, select Create Custom Data and enter activity logon
    Input Parameter: parameter, select Empty Text
  • Save the automation

Step: Test

  • Select Test
  • Should display Test session ended successfully and show the output in the Test Console

Add activity to delete Team

We will add the activity to the JavaScript class and the automation.

To maintain your SDK properly, you should also add the activity to the Data Type cs_activities_SAC and add the new activity to the Enumeration (logon,addTeam,logoff,addTeam).
But this is not needed for our test scenario.

Step: JavaScript class

  • Edit the automation cs_package_SAC
  • Select Step Define Activities Class
  • Select Edit Script
  • Before the line async logoff() {
    insert the code below

        async deleteTeam(parameter) {
            this._fnActivityCore.log();
            const options = await this._requestOptions({          
                url : configSAC.tenantScimURL + 'Groups/' + parameter.name,
                method : 'DELETE',
                useCsrfToken: true,
            });
            const response = await irpa_core.request.call(options);
        }
  • You script should contain the new method
  • Save the automation

Step: Automation

  • Edit the automation test (created in previous step)
  • Create a variable with Data Type String
    – select from Data – Data Types String and drop to the flow diagram
    – use name Set parameter Team
    set value with the JSON: {“name” : “RPA_Team_01”}
    set Output Parameter paramTeam
  • Add the automation cs_activity_SAC to your flow (2 times)
  • Save the automation
    sometimes you have to close the automation and open again, cause parameter values will not be active for next steps
  • Select the step after Set parameter Team
  • Use Step name Activity – add Team
    Input Parameter: packageSAC, select packageSAC from the list
    Input Parameter: activitySAC, select Create Custom Data and enter activity addTeam
    Input Parameter: parameter, select paramTeam
  • Select the next step
  • Use Step name Activity – delete Team
    Input Parameter: packageSAC, select packageSAC from the list
    Input Parameter: activitySAC, select Create Custom Data and enter activity deleteTeam
    Input Parameter: parameter, select paramTeam

Step: Test

  • Select Test
  • Should display Test session ended successfully and show the output in the Test Console

Step: Check in SAP Analytics Cloud

  • The team was created and deleted in your SAC tenant, so you can*t find it in the list of your teams
  • But the SAC API is creating a team folder, so you can check if the folder RPA_Team_01 exists
  • Important: Even the team is deleted, you cannot create the same team again without deleting the team folder first.

 

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.