Skip to Content
Technical Articles
Author's profile photo Vishnupriya Varadharajan

Invoking Business Rules from custom UI5 application in BAS

Overview

This blog post will cover the steps to invoke a Business Rule from a custom UI5 application using Business Application Studio in Cloud Foundry environment of SAP Business Technology Platform.

Disclaimer: I do not intend to explain about Business Rules concepts in this blog or how to create and deploy them in BTP CF. There are quite a few blogs beautifully explaining them. You can follow the blog from Murali Shanmugam to configure Business Rules in CF. I have used the same as a sample to invoke them from ui5 app.

 

Pre-requisites

  • Please make sure your BTP account is set up with Boosters for Workflow Management. Destination BUSINESS_RULES is automatically configured as a result of the Booster set up. We will be using this in our SAP UI5 app.
  • BAS set up is done and respective roles are assigned
  • Business Rules created and deployed in CF

 

Steps to Invoke Business Rules

Step 1: Create a mta project and add Fiori module to it

  • Create a mta project using the command yo basic-multitarget-application

  • Now, lets add a fiori app to this mta project. Go into the project we just created and add a fiori module using the command yo fiori-module and fill in all the information required :

Create%20Fiori%20Module

Create Fiori Module

 

Step 2 : Update mta.yaml file with Business Rules Service Instance

Go to mta.yaml file and include the business rules service instance which was automatically created with the help of Booster set up of Workflow Management. (wm_businessrules is the name of my instance)

Business%20rules%20instance%20in%20mta

Business rules instance in mta

Step 3 : Update xs-app.json of Fiori Module

Next, update the route in your Fiori Module application. For that, open xs-app.json file of your fiori module and add this route. Make sure you add this route before the generic routes.

  • Also ensure that the “authenticationMethod” is “route” (check line 3 in screenshot)
        {
            "source": "^/BUSINESS_RULES/(.*)$",
            "target": "$1",
            "destination": "BUSINESS_RULES",
            "authenticationType": "xsuaa"
        },

xs-app.json

 

xs-app.json

Step 4 : Update Controller.js of Fiori Module to call Business Rules APIs

Update Controller.js file to call business rules-service API. Before calling the API to invoke the business rule, you need to first get the xsrf-token and then call the API.

  • To do so, you will also need information of the application ID that will be prefixed with API host URL. You can get this information from the manifest.json file inside your fiori module as sap.app –> id. Application ID is the ID without dot.

For Example: In below example the sap.app –> id is ns.fioriBRmodule so the                                                            application ID becomes nsfioriBRmodule

manifest.json

manifest.json

  • Now you need to update the code in Controller.js to first make a GET API call for XSRF  token
     /applicationID/BUSINESS_RULES/rest/v2/xsrf-token
    
    For eg :
     /nsfioriBRmodule/BUSINESS_RULES/rest/v2/xsrf-token​

    X-CSRF-Token

  • Next, you will use this xsrf-token to make the POST API call to invoke the Business Rule. You will need to pass the body which contains the rule service id and the data needed for the business rules to be executed (know more about the APIs from here). I have used V2 apis here as version1 apis are deprecated.

 

/applicationID/BUSINESS_RULES/rest/v2/workingset-rule-services
For eg : 
/nsfioriBRmodule/BUSINESS_RULES/rest/v2/workingset-rule-services
 var jsonData = {
                    "RuleServiceId": "XXXXXX",
                    "Vocabulary": [
                        {
                            "Customer": {
                                "CreditRating": "AAA",
                                "AvgOrderValue": 3000
                            }
                        }
                    ]
                }
//Then invoke the business rules service via public API
                        $.ajax({
                            url: " /nsfioriBRmodule/BUSINESS_RULES/rest/v2/workingset-rule-services",
                            method: "POST",
                            // contentType: "application/json",
                            data: JSON.stringify(jsonData),
                            async: false,
                            headers: {
                                "X-CSRF-Token": token,
                                "Content-Type": "application/json"
                            },
                            success: function (result1, xhr1, data1) {
                                if (result1 != null) {
                                    alert("post success");
                                    //info: the output from rules will be wrapped in result1 object, you can 
                                    // access this json object to get the output variable.
                                    ;
                                }
                            }
                        });

 

Step 5 : Build and Deploy the application

  • Right click on the mta.yaml file to Build the mta project
  • Once successful, right click on the generated .mtar file and Deploy to CF

Step 6 : Test the application

Finally, test the application from the SAP BTP cockpit. Pls append the application ID to the app link.

For eg  : https://xxxxx.hana.ondemand.com/nsfioriBRmodule/index.html

 

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Pankaj Kamble
      Pankaj Kamble

      Hi Vishnupriya,

       

      i have followed all the steps and deployed Application on trail Account,

       

      Unable to Open Application as path / URl you given, My Application URl not working.

       

      could you please help me.

       

      Regards,

      Pankaj