Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Rama-Murari
Product and Topic Expert
Product and Topic Expert
3,302

Introduction :

  • Allows users to write executable code in JavaScript, leveraging pro-code capabilities for fusion development of enterprise automation scenarios.
  • Enables the execution of complex data type transformations and additional operations on the process context.
  • Previously it was limited to automation, now offers access to the entire context, including process inputs and custom variables, facilitating advanced data manipulation and transformation.
  • This activity comes with a new Script Editor which gives perfect IDE experience for user

Script Task Editor Features

  • Microsoft's Monaco based JS Code Editor
  • IntelliSense Support
  • Syntax highlighting
  • Type awareness
  • Console Panel- Collapsable & Expandable
  • Error Navigation
  • Other Editor Features:
    • Cut, copy, paste
    • Find all references
    • Go to definition
    • Code refactoring

Process Builder and Process Runtime  Restrictions

  • Allow till ES 5.1 standards
  • Restrict 'eval' and 'Function' constructor
  • Restrict direct manipulation of $.context, $.info, $.usertasks, allows only read
  • Read-only of input variables in context
  • Write possible only for custom variables in context
  • No addition/modification/deletion of input variables in $.context
  • Max 10,000 characters

Typical Use cases of Script Task:

  1. Array Iteration
  2. String manipulation
  3. Fetch Business Approval Status

Sample Scenario :

Lets take the below project scenario and see how Script task can come in handy.

RamaMurari_0-1727686472884.png

  1. Fetch list of products from a backend system like S/4HANA 
  2. Iterate through the list of products and find the total price of all the items
  3. Concatenate Price and Currency of items which are otherwise coming from backend as two different attributes
  4. Send the total price and concatenated price and currency details as well to the approver for approval
  5. Fetch the business approval status (approved or rejected)
  6. Send Email to processor who has started the process with the approval details

The highlighted steps (ArrayList Iteration Script Task and Approval Status Script Task) are the steps pertaining to Script Task which will help with transforming the data , to find the total price of all items and concatenate price and currency (points 2 and 3 above).

Lets see in detail how the Script Tasks in this sample scenario has been created below :

  1. Custom variables are created to write the manipulated data and use it across the process 
  2. Process Input variables can be read but cannot be written to using Script task
  3. Process context up to previous steps can be read from the left side panel directly 
  4. When the context variables including Process inputs or Custom variables are chosen from the left side panel, automatically java script code for that gets populated in the right side of the editor where the code is actually written
  5. Script Editor comes with some sample code snippets to enable CzD also to start coding in java script.
  6. Intellisense support will help the user with recommendations as he types

Array Iteration Script Task:

  1. The items received from the previous Actions API call can be iterated through and the price of each item is summed up and total price is calculated 
  2. In the same iteration , the attribute values of price and currency are concatenated so that it can be sent across to the next step in the process which is the approval form

Array Iteration Script Task.gif

Fetch Approval Status Script Task:

  1. Business Approval status of whether the approval task has been approved or rejected can  be determined with this Script task
  2. $.usertasks ,$.info and $.roles allow access to identifiers of the current task or the exact execution.For more information on this, you can refer here
  3. We will make use of $.usertasks to fetch the business approval status
  4. $.usertasks has to be followed by the approval formId which can be obtained from intellisense or by clicking on one of the attributes of the approval form from left panel and replacing $.context with $.usertasks

Business Approval Status Script Task.gif

Isolated Code Execution - Testing

  • Easy to test written code even without process deployment.
  • Users can provide sample input values for the context variables used in the script.
  • Upon clicking the "Run Test" button, the code will be executed in the browser's JavaScript engine, and the result will be displayed instantly. 

Testing.gif

 You can refer to the YouTube video on the same topic here for more details. 

Whats coming up - Further Enhancements :

  1. Generating the javascript code using Generative AI capability
  2. Reuse of Scripts

Happy Scripting and Transforming Data as per your need 😊

 

 

 

1 Comment