Motivation
SAP Build Process Automation just got even more powerful with its latest feature that allows users to write executable code in JavaScript. This new feature, introduced in October 2024, enables users to perform specific calculations or data manipulations as part of their business process.
Previously, the ability to write executable code was limited to automation tasks, but now, users have access to the entire context of their business process. This means you can manipulate data and perform calculations at various stages of the process, making it more dynamic and responsive to changing needs.
Creating a business project from scratch may seem like a daunting task, but with the right starting point and guidance, it can be a seamless process. In this blog post, I will guide you through the steps to create a business project that includes an action task, a script task, and an approval form to facilitate a simple approval workflow.
Scenario
Imagine a sales team that wants to offer tailored discounts to repeat customers. They need to present these discount requests to the finance department for review and approval, ensuring discounts align with business policy. Here’s how this process typically unfolds:
Note: The simplified implementation of this scenario is designed to showcase the usage of a script task within a business process.
Prerequisites
Getting Started
Let’s break down the scenario into its components and examine how to create the necessary artifacts.
Step 1: Create a SAP BTP Destination
The data required for this process will be retrieved from the OData Northwind service, which offers access to a sample database with product and customer information. We need to set up a SAP BTP Destination that will be utilized by an Action Task in the SBPA project.
Property | Value |
sap.applicationdevelopment.actions.enabled | true |
sap.processautomation.enabled | true |
sap.build.usage | odata_gen |
Step 2: Create, Test and Publish an Action Project in SBPA
In this step, you will create an Action Project that utilizes the BTP destination. This Action Project will be published to the action library and will be accessible in the SAP Build lobby. Later, this Action Project will be used to retrieve products from the OData Northwind service.
Step 3: Test, Release and Publish your Action Project
In the Actions Editor you can choose the methods that you would like to publish as Actions. Choose Get entities from Products.
Before releasing, you can test the Action Project. You should receive a list of products from the OData Northwind service.
Step 4: Create a new Business Process Project
Select the added Action in the Process Canvas and create a destination variable with any name. This variable will be used during deployment of the process to link to an actual destination. A destination variable enables linking different destinations in different landscapes ( Dev, Test, Prod) without changing the Process.
.
Step 5: Create an API Trigger
We are going to use an API Trigger to initiate the workflow. In the Process Editor click on Add a Trigger select API Trigger add a name and click Create.
Name | Type |
itemresults | get_Products_200_output_schema |
totalprice | Number |
totaldiscountprice | Number |
Step 6: Add and configure the Action Project
Input Field | Input Value |
Identifier | Northwind |
Type | Select Destination |
We want to restrict the number of items from the results that come from the OData Northwind service. Therefore we use the productitems variable. Choose the Input tab and add productitems as value for $top:
Step 7: Create a Script Task
We are now incorporating a simple logic into our process. Our goal is to calculate the total price and the total discounted price for products obtained from the OData Service.
In the Process Editor click on (+) icon after the Action Task. Add a Script Task
Double click on the Script Task to open the Editor. Delete the code and add the following code:
$.context.custom.totalprice = 0;
var result = $.context.action_get_Products_1.result.value;
var discountprice = 0;
result.forEach(function (value) {
$.context.custom.totalprice += Number(value.UnitPrice);
})
discountprice = ($.context.custom.totalprice * ($.context.startEvent.discount / 100));
$.context.custom.totaldiscountprice = $.context.custom.totalprice - Number(discountprice);
You have the option to test your JavaScript code locally by using sample values for the context variables, all without having to deploy the project. To do this, simply switch to the Test Variable tab and input default values for the context variables that are being used. For instance:
Variable Name | Variable Value |
$.context.action_get_Products_1.result.value | [{"CategoryID": 1,"Discontinued": false,"SupplierID": 1,"UnitPrice":"18.0000","ProductName": "Chai","UnitsOnOrder": 0,"QuantityPerUnit": "10 boxes x 20 bags","ProductID": 1,"ReorderLevel": 10,"UnitsInStock": 39}, {"CategoryID": 1,"Discontinued": false,"SupplierID": 1,"UnitPrice":"19.0000","ProductName": "Chang","UnitsOnOrder": 40,"QuantityPerUnit": "24 - 12 oz bottles","ProductID": 2,"ReorderLevel": 25,"UnitsInStock": 17}] |
$.context.custom.totalprice | 0 |
$.context.startEvent.discount | 25 |
$.context.custom.totaldiscountprice | 0 |
Click Run test
You can find more information about testing: here
Click on Apply and Save
Step 8: Create a Approval Form
In the last step, we will design a simple approval form to either approve or reject the product discount.
Form Fields Value | Field Type | Configuration (Read Only) |
Approve Products and Discount | Headline1 | x |
A new approval task has been received. Please review and confirm whether the discount can be met or not. | Paragraph | x |
Product list | Table | x |
Product Name | Text | x |
Unit Price | Text | x |
Prices | Table | x |
Discount % | Number | x |
Total Price | Number | x |
Total Discount Price | Number | x |
Message to Seller | Text Area |
In the Subject section:
Save your work
Congratulations on successfully completing your project! Your process should look like below:
Step 9: Release the Business Process Project
In order to start the process, you must first release the business process project and then deploy it. Releasing the project creates a version or snapshot of the changes, while deploying the project makes it available in runtime for use. It is only possible to deploy a released version of the project, and at any given time, there can be multiple deployed versions of the same project.
Step 10: Run the Business Process
{
"productitems": 6,
"discount": 35
}
Step 11: Accessing the task
Now it's time to monitor the process flow and access the tasks.
Tasks are the request for the users to participate in an approval or review process. These tasks appear in the My Inbox application shipped with SAP Build. Users can claim, approve and reject the task from their inbox. You can add a message to the requester.
Users can approve and reject the task from their inbox and you can add a message to the requester. Additionally, you could consider adding more steps to the process, such as sending a notification email to the requester.
Additional Read
To learn more about the topic covered in this blog post, please also refer to the following assets & follow us on SAP community.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
27 | |
26 | |
14 | |
14 | |
13 | |
11 | |
10 | |
7 | |
7 | |
7 |