Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
TJe
Product and Topic Expert
Product and Topic Expert
In this tutorial, I will share, how you can develop custom script Activities with JavaScript for your API. This is a generic template/pattern you can adopt for APIs of different products.

Main goals are

  • support the Low-Code/No-Code approach with additional Activities
    mainly for Citizen Developers or Business Process Experts, they will use the final package

  • support the development of custom scripts with a template / design pattern
    for Developers, they will develop and maintain the package and can see several concepts for SAP Intelligent RPA and JavaScript in action


The idea is based on the experience of custom script development from other projects (e.g. Use SAP Analytics Cloud REST API or Use SAP BusinessObjects REST API). Even with the a small set of custom scripts, there is duplicated code and adding features would make this even more worse and leads to code which is hard to maintain.

Tutorial Blogs



  • Overview and Concept (please read this first)
    Getting started

  • Custom script SDK – Design Pattern (this blog)
    Overview, Concepts and Step by Step guide

  • Custom script SDK – Rest API sample
    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


Concepts


We will learn the concepts of the design pattern, create the project, implement a sample API, test the SDK, generate and share a package.
The sample API is very simple, the methods init, execute (with parameters) and done will only write messages to the console.

Here the design pattern concept with dependencies and how it can be used for APIs:




  • JavaScript
    Classes seems to be a good idea to implement the SDK in one custom script
    Functions in variables can reduce duplications
    Define async functions and use await, e.g. useful when using REST APIs and HTTP Request to wait till an asynchronous function is returning a value

  • SAP Intelligent RPA
    Data Types are a good way to make the use of Parameters simpler and more structured (input, output, activity)
    Automations are used to get an Activity like usage of the scripts
    a naming convention is used to identify content of the SDK (prefix cs_ for Custom Script SDK)


Of course there are always different ways to implement solutions, so feel free to adopt and enhance.

Overview


To test the pattern, we will create demo automations for No-Code and Low-Code



Content List























































Area Name Description
Data Type cs_class instance of the JavaScript class with Activities
cs_package package content, currently only activites (cs_class)
cs_activities_Sample enum with all activity names of the Sample API
cs_param_Sample_Execute example of a parameter definition for the activity execute
Automation cs_function_Core custom script, returns a JavaScript function ActivityCore() that can be stored in a variable
cs_package_Sample reuse the function ActivityCore(), custom script with the implementation of the API in the class Sample, save the activities in the package variable
cs_activity_Sample call the specified activity from the package, activity and parameter are defined as input parameters.
Used as activity in your automations.
Demo_SampleSDK Demo automation of the SDK for no-code usage
Demo_SampleSDK_Expert Demo automation of the SDK for low-code usage

Create a New Project




  • Use the Project name: Custom Script SDK Sample & Design Pattern

  • Use the Description:
    Content for custom APIs - similar to SAP Intelligent RPA SDKs - reuse the template to develop your custom script ACTIVITIES with JavaScript leveraging your APIs


Create Data Types


Step: Create Data Type




  • Use the name cs_class

  • Define the fields of the data type as show in the picture below
    – use New Field to add a new row

  • Select Save


Step: Create Data Type



  • Use the name cs_package

  • Define the fields of the data type



  • Select Save


Step: Create Data Type



  • Use the name cs_activities_Sample

  • Define the fields of the data type
    for the Enumeration, add the name of the activities: init,execute,done
    (the allowed and implemented activities for your API)

  • Select Save


Step: Create Data Type



  • Use the name cs_param_Sample_Execute

  • Define the fields of the data type

  • – use New Chield to add a new entry for details

  • Select Save


Automations - Define Design Pattern


Automation - cs_function_Core


Define a Custom script which returns a JavaScript function ActivityCore() that can be stored in a variable.

Step: Create Automation



  • Use the name cs_function_Core

  • Select I/O to define an Output parameter
    The automation will return a JavaScript function and we will use the function in other automations as input parameter
    name: cs_function_ActivitiesCore, 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 Define Activities Core

  • Select Edit Script

  • Select Add new output parameter

  • Enter Output parameter name functionDef, select type Any

  • Copy the following script code to line 1
    return function ActivityCore(name) {
    this._name = name;
    this._activity = '';

    this.call = async function(caller, activity, parameter) {
    this._activity = activity;
    // if you specify a String in JSON format --> JSONObject
    if (typeof parameter === 'string' || parameter instanceof String) {
    if (parameter.length > 0) parameter = JSON.parse(parameter);
    }
    try {
    delete parameter.$_jsonSchemaCons; // IRPA data types, not needed as parameter value
    } catch (e) {
    }
    this._parameter = parameter;
    await caller[activity](parameter);
    };

    this.log = function() {
    irpa_core.core.log(this._name + '.' + this._activity + (this._parameter ? '(' + JSON.stringify(this._parameter) + ')' : ''));
    };
    };


  • Close the Script View (X Icon on top right corner next to Save, or just click an empty area in the canvas)

  • Select Define Activities Core in the flow diagram
    Output Parameters should contain the name functionDef


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 functionDef from the list (n the custom script we have returned the function in this variable)

  • Save your automation


Automation - cs_package_Sample


Reuse the function ActivityCore(), define a custom script with the implementation of the API in the JavaScript class Sample and save the activities definition in the package variable

Step: Create Automation



  • Use the name cs_package_Sample

  • Select I/O to define an Output parameter
    The automation will return the package definition, we will use in other automations as input parameter
    name: cs_SamplePackage, select type cs_package from the list

  • Save the automation


Step: Add Automation



  • Add the automation cs_function_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

  • Select Add new input parameter

  • Select Add new output parameter

  • Enter Input parameter name fnActivityCore, select type Any

  • Enter Output parameter name activities, select type cs_class

  • Copy the following script code to line 1 (only sample code to show the concept)
    /**
    * @description enter the description for your Sample SDK, which API is used, ...
    */
    class Sample {

    constructor() {
    this._fnActivityCore = new fnActivityCore('Sample');
    }

    async call(value, parameter) {
    await this._fnActivityCore.call(this, value.activity, parameter);
    }

    /**
    * @activity init - Sample
    * @note description of your activity
    */
    async init() {
    this._fnActivityCore.log();
    }

    /**
    * @activity init - Sample
    * @note description of your activity
    * @param description of parameter
    */
    async execute(parameter) {
    this._fnActivityCore.log();
    }

    /**
    * @activity init - Sample
    * @note description of your activity
    */
    async done() {
    this._fnActivityCore.log();
    }

    }

    return new Sample();​


  • Close the Script View (X Icon on top right corner next to Save, or just click an empty area in the canvas)

  • The red marker is an indicator, that the parameters are missing
    Select the step in the flow
    for parameter fnActivityCore, select cs_function_ActivityCore from the list (output from the previous step)
    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
    Use Output Parameters name cs_package

  • Select Edit Activity
    For the parameter instance select activities from the list

  • For Output Parameters set 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_Sample


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_Sample

  • Select I/O to define the three Input parameters
    name: cs_package_Sample, select type cs_package from the list
    name: cs_activity_Sample, select type cs_activities_Sample 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

  • Select Add new input parameter
    3 times, we will pass all automation parameters to the script
    name: activities, select type cs_class from the list
    name: activity, select type cs_activities_Sample 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)

  • The red marker is an indicator, that the parameters are missing
    select the step in the flow

  • Save your automation

  • For parameter activities, enter cs and select cs_package_Sample.activities from the list
    (the list will not show the entry without typing 'cs')

  • For activity select cs_activities_Sample from the list

  • For parameter select parameter from the list

  • Save the automation


Automations - Test Design Pattern


We are using the activities init, execute and done of our sample API in automations using

  1. custom scripts (low-code)

  2. reuse an automation (no-code)


You can try to build the automations of your own or follow the step by step instructions.

Test input parameter


You can select the activity as input parameter from the list:

Test output


The console should show the same execution logs, cause both are calling the activities of the sample API

Automation Low-Code - use Custom scripts


To reduce the definition effort, we will create copies of the Custom Script activity in our flow.

Step: Create Automation



  • Use the name Demo_SampleSDK_LC

  • Select I/O to define an Input parameter
    The automation will allow to select an activity as input parameter
    name: activity, select type cs_activities_Sample from the list


Step: Add Automation



  • Select Tools and add automation cs_package_Sample
    Select the activity in the flow diagram
    Use step name: Define Package - Samples


Step: Add Custom Script



  • Add the activity Custom Script to your automation
    Select the activity Custom script in the flow diagram
    Use Step name Activity
    Select Edit Script

  • Select Add new input parameter
    2 times, we will pass all automation parameters to the script
    name: activities, select type cs_class from the list
    name: activity, select type cs_activities_Sample from the list

  • Copy the following script code to line 1
    await activities.instance.call(activity);




  • Save the automation

  • Close the Script View (X Icon on top right corner next to Save, or just click an empty area in the canvas)

  • Select Activity in the flow

  • For parameter activities, enter cs and select cs_SamplePackage.activities from the list
    (the list will not show the entry without typing 'cs')


Step: Duplicate Custom Script



  • Duplicate the activity 3 times (Context menu - Duplicate or Ctrl+D)
    Set the step names, you should have
    Activity - InputParameter
    Activity - init
    Activity - execute
    Activity - done

  • Select step Activity - InputParameter
    Input Parameters - activity: select activity from the list

  • Select step Activity - init
    Input Parameters - activity: select Create Custom Data, enter activity value: init

  • Select step Activity - done
    Input Parameters - activity: select Create Custom Data, enter activity value: done


Step: Create parameters variable



  • select from Data – Data Types cs_param_Sample_Execute and drop to the flow diagram (before Activity - Execute)
    use Step name; Set param - execute
    select Edit Activity
    set values for name, code and description (myExpert, 1111, myDescription)
    change the Output parameters name to paramExecute

  • Save the automation

  • Select the step Activity - execute
    Input Parameters - activity: select Create Custom Data, enter activity value: execute

  • select Edit Script
    select Add new input parameter
    name: parameter, select type cs_param_Sample_Execute from the list

  • change the script code to
    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 the automation

  • Select Activity - Execute in the flow

  • Input Parameters - parameter: select paramExecute from the list

  • Save your automation


Test 



  • Select Test

  • Select environment and Input Parameters - activity from the list

  • Console should show


Automation - No-Code: reuse automation


We will use a copy of the low-code automation to reduce the definition effort.
In the current version it is not possible to copy an automation activity in a flow (standard activities can be copied).

Step: Duplication Automation



  • From Automations - Select Demo_SampleSDK_LC

  • Open the context menu (right click) - Select Duplicate
    use the name Demo_SampleSDK_NC
    select duplicate


Step: delete activities



  • Delete the custom script activities from the flow
    Activity - InputParameter
    Activity - init
    Activity - execute
    Activity - done

  • you should have / keep the steps
    Define Packages - Samples
    Set param - execute


Step: Add Automation



  • add the Automation cs_activity_Sample to your flow (after Define Package - Samples)
    select the step in the flow, use name Activity - InputParameter
    Input Parameters - cs_package_sample: select cs_SamplePackage from the list
    Input Parameters - cs_activity_sample: select activity 
    Input Parameters - parameter: select Empty Text from the list

  • add Automation cs_activity_Sample (after Activity - InputParameter)
    use name Activity - init
    Input Parameters - cs_package_sample: select cs_SamplePackage
    Input Parameters - cs_activity_sample: select Create Custom Data, enter activity value init
    Input Parameters - parameter: select Empty Text from the list

  • add Automation cs_activity_Sample (after Set param - execute)
    use name Activity - execute
    Input Parameters - cs_package_sample: select cs_SamplePackage
    Input Parameters - cs_activity_sample: select Create Custom Data, enter activity value execute
    Input Parameters - parameter: select paramExecute

  • add Automation cs_activity_Sample (after Activity - execute)
    use name Activity - done
    Input Parameters - cs_package_sample: select cs_SamplePackage
    Input Parameters - cs_activity_sample: select Create Custom Data, enter activity value done
    Input Parameters - parameter: select Empty Text

  • Save the automation


Test 



  • Select Test

  • Select environment and Input Parameters - activity from the list

  • Console should show


Generate and Share the Package


Last part is now to generate and share our package, so it can be reused on other projects.

Step 1 - Generate



  • Select Overview on your project

  • Select Generate Package

  • Use the name: Custom script SDK Sample & Design Pattern

  • Use the Description:
    Content for custom APIs - similar to SAP Intelligent RPA SDKs - reuse the template to develop your custom script ACTIVITIES with JavaScript leveraging your APIs

  • Select Generate Package


Step 2 - Share



  • In the Factory - Select Packages

  • On the package select More Options and Share

  • On Share Package set Share with: Anyone and Authorization: Read, click on Share

  • Your Package is now shared and can be used in Projects


Test the shared Package


Create a New Project



  • Use Project name: Custom script SDK - Test

  • Select Dependencies – Manage Dependencies

  • Select Other and click Add Dependency

  • Select the Package 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


Automation - Create and Test


You can create the same test automations as described above for low-code and no-code or create this simple sample.

  • Select Create - Automation

  • Use the name Test

  • Select Tools and add automation cs_package_Sample
    Select the activity in the flow diagram
    Use step name: Define Package - Samples

  • add the Automation cs_activity_Sample
    select the step in the flow, use name Activity - init
    Input Parameters - cs_package_sample: select cs_SamplePackage from the list
    Input Parameters - cs_activity_sample: select Create Custom Data, enter activity value init
    Input Parameters - parameter: select Empty Text

  • Save the automation

  • Select Test

  • Console should show


 

Conclusion


You have seen several concepts for JavaScript and SAP Intelligent RPA. The Design Pattern can be used as a template for the development of a Custom script SDK for your API (like the samples listed under Part 2 above. e.g. the Custom script SDK for the SAP Analytics Cloud API).

 
2 Comments