Skip to Content

 

Pattern 1 – In App Extensions for Cloud Applications

This is the first blog of my blog series on Business Rules Consumption Patterns. In this Pattern 1, I will explains how business rules can be used directly and indirectly in your custom applications (which can be deployed on-premise or on-cloud)

Usage Scenario #1

Use SAP Cloud Platform Business Rules in your custom applications

Business Rules can be used directly in custom applications to extend backend business logic that varies often for functional reasons. For that you model, activate and deploy the business rules from web-tool and then use the REST-based rule service APIs to invoke the rules in your application.

  • Follow the blog from to learn how to author and deploy the rules in SAP Cloud Platform.
  • You can also try and build rules on your own in your SAP Cloud Platform trial account.
    • Refer my blog to enable to the business rules service in SAP Cloud Platform

Once you have modelled and deployed your business rules in cloud, all you need to do is use Public RESTful APIs to invoke the rules in your application.

As an example, I will show you how you can call the rule service in SAPUI5 application (in javascript based controller file). But before I explain that let me give you a glimpse of rule artefacts like project, data objects, ruleset, rule and rule-service that are already modelled, activated and deployed in my cloud account. (for more information on what these various rules artefacts and how they are related, refer to the official documentation)

The rule used in the example is a discount rule – which calculates the discount on the Product based on Quantity and Category of the product.

Product: Data object that is used as input to rules service

 

DiscountOutputDO: Data object that is used as output to rules service

 

DiscountRuleservice: This service is deployed in cloud platform and is available as RESTful service which will be used to invoke from the application. Note the Input and Result data object.

 

DiscountRules: Decision table based rules with conditions based on Product data object and DiscountOutputDO as result data object:

 

Now, let us see how you can invoke the rule service (DiscountRuleservice) from your HTML5 application. It involves 2 steps: First call the API to get CSRF token and then call the service to invoke the business rules.

// First get the CSRF token
$.ajax({
    url: "/bpmrulesruntime/rules-service/v1/rules/xsrf-token",
    method: "GET",
    headers: {
      "X-CSRF-Token": "Fetch"
    },
    success: function(result, xhr, data) {
       var token = data.getResponseHeader("X-CSRF-Token");
       
       //Then invoke the business rules service via public API
       $.ajax({
           url: " /bpmrulesruntime/rest/v1/rule-services/ShoppingCartPromotionRules/DiscountRuleservice",
           method: "POST",
           contentType: "application/json",
           data: InputPayload,
           async: false,
           headers: {
             "X-CSRF-Token": token
           },
           success: function(result1, xhr1, data1) {
               if (result1 != null) {
                   //info: the output from rules will be wrapped in result1 object, you can 
                  // access this json object to get the output variable.
                  result1[0].Discount;
               }
           }
       });
    }
});

 

Few important things to note here:

  1. First you need to get the CSRF token while using the public API.
  2. In the URL, you will notice bpmrulesruntime – it is the destination that is already configured if you enable business rules service in your account. If your application is deployed in the same account as business rules then just having bpmrulesruntime in the URL would suffice, else you need to create the destination and use them appropriately via neo-app. json file of your application, as shown in the screenshot here:

 

  1. After you successfully get the CSRF token, then you need to make a POST call to invoke the business rules using public API. Here are the details of the parameters:
    1.   content Type: is mandatory and have to be application/json
    2.   data: is the JSON payload matching the Input data type of the rule service.

From the example above, the Input data type of the rule service (DiscountRuleservice) is Product with attributes Name, Category, Quantity, Soldby and Price then your InputPayload to the ajax call would be:

{
    "__type__":"Product",
    "Category":"Flat Screen TVs",
    "Quantity":40,
    "Soldby": "Very Best Screens",
    "Price":2000
}

Note the __type__ field. This is mandatory attribute to be provided as input payload to business rules. It is nothing but the data-type name. Additionally, the JSON element names have to be exact match of the attributes of the data types (!!! Even a lower-case ‘b’ in Soldby could cause problem !!!)

Refer the API documentation for more details on other parameters

  1. Result after invoking the rule can be obtained in the success method. The result is a JSON structure that matches the result data-type of the rule service

From the example above, the result data-type associated with the rule service is DiscountOutputDO with attributes Discount then the result payload structure will look like:

{
    "__type__": " DiscountOutputDO ", 
    "Discount": 10
}

Suggestion: I would suggest running the rule service once with the actual payload in POSTMAN or any other rest client to be sure of the header parameters and the input and output payload you are expecting.

 

This completes the first blog of the series. In this blog, I focussed on Consumption Pattern #1 and showed you how-to consume business rules service using public APIs from your custom application. The consumption involved 3 main steps (a) configuring the neo-app.json for bpmrulesruntime destination (b) getting the xsrf token and (c) invoking the service. Setup and use the sample application to learn more about this consumption pattern.

Sample Reference Application

For reference, you can download sample rules project from GitHub and import it in your cloud platform account. To do so,

  • first import ShoppingCartPromotionRules.zip rules project in your business rules editor & deploy the DiscountRuleservice and then deploy the shoppingcart.zip application

(Follow the README instructions to setup the sample rules and application)

 

Related Blogs
SAP Cloud Platform Business Rules – Extensions and Consumption Patterns
Embed Business Rules SAPUI5 control in your custom applications
Using Business Rules in SAP Cloud Platform Workflow

To report this post you need to login first.

23 Comments

You must be Logged on to comment or reply to a post.

  1. Former Member

    Hi Archana,

    Thanks for your blogs. I also read the blog of Murali about use Business Rules Service via a Fiori APP. I find something different between you and him.

    It’s the one way to invoke the Business Rules in your blog:

    It’s the another way to invoke Business Rules in Murali’s project which he deployed to Github:

    At first I try to implement this project within Murali’s codes, but it didn’t work. Could you please tell me why do you and Murali use different APIs? It is important for me to find out why I can’t implement the project by myself..

    Could you please let me know when you feel free?

    Best Regards,

    Joy Lee

     

    (0) 
      1. Former Member

        Hi Archana,

        Thanks for your help, today I run the project from your Git, it worked actually.

        Till now, I have the last question: in the other blog of yours, I saw that you have mentioned about “Text Rules” and “Flow Rules”.

        Are they new function about Business Rules & Workflow? Where can I find more information about them? I googled them but found nothing. I’ll appreciate for your advice.

        I’d very glad to discuss with you when you feel free next time.

        Best Regards,

        Joy Lee

        (0) 
        1. Archana Shukla
          Post author

          Hello Joy Lee,

          Text Rule and Flow Rule are different rule modelling variants which serves different purpose. Text Rule are like If-Then-ElseIf-Else type of rules and Flow Rule are rules helps to sequence rule execution like calling multiple decision tables/text rules in an order (-It does much more as well-).

          Both Decision Table and Text Rule types of rule can be modelled from SAP Cloud Platform Business Rules. You can find more in official documentation:
          https://help.sap.com/viewer/9d7cfeaba766433eaea8a29fdb8a688c/Cloud/en-US/1631dd39f7ae42f189b74aeee5bb18d9.html

          Besides this, you can also embed decision table and text rule controls in your custom application. Find more information on these SAPUI5 control here:

          https://sapui5.hana.ondemand.com/#/api/sap.rules.ui.RuleBuilder/constructor

          Hope that helps.
          Please let me know if you need more information on them.

          Regards,
          Archana

           

          (0) 
          1. Former Member

            Hi Archana,

            Thanks for your answer. I read the official document and realize the difference between Text Rule and Decision Table.

            In the next blog of yours, I learned how to embed decision table into a SAPUI5 APP. After I implement this task, I think it is a convenient decision table editor, but I don’t if there is more things it can implement.

            so if there is a task like this:

            • I need to get an output data object in programming then use it to work with other services or application

            I know other way to implement this task like use Workflow or use Postman with public REST APIs.

            But I’m very curious about if this APP which be embedded Decision Table can do any part of this task?

            Thanks for your reply again, any advice will be helpful for me.

            Best Regards,

            Joy Lee

             

            (0) 
            1. Archana Shukla
              Post author

              Hello Joy Lee,

              There are two parts of the story. One is modelling rules and other is executing/invoking rules. What you do via Embedded decision table/text rule control is about “modelling” rules and that is done via public rules repository/authoring APIs. Once the rules are modelled and deployed, it can be executed/invoked from any application using public rules runtime/execution APIs. You can get more information on both of the APIs here

              If I understand your question correctly, you can get the output of the rules after invoking it via the public APIs and then send it as body or input to any other service.

              Regards,
              Archana

              (1) 
              1. Former Member

                Hi Archana,

                Thank you for your explanation. As your guide I’m going to create a APP to implement this task.

                Thanks for your help!! Hope you have a nice day!

                Best Regards,

                Joy Lee

                (0) 
      2. Anurag Garg

        Hi Archana,

         

        I am getting error while deploying my business rule service. But can’t find an error anywhere. Can you please help to guide me where do I need to check deployment errors ?

        (0) 
  2. Former Member

    Hi Archana,

    I have implemented a APP to invoke Business Rules service as your guide, but I find some codes confused in app.controller.js of your blog:

    When I use the url which is same with yours, it provided me a error below.

    Then I try another url which is used to invoke Business Rules Service in Workflow Service Task, it does work unexpectedly:

     

    So I suspect maybe something about the url has changed but we haven’t noticed it..

    Please let me know if you have any advice when you feel free, Thank you.

    Best Regards,

    Joy Lee

     

    (0) 
    1. Archana Shukla
      Post author

      Hello Joy Lee,

      The later one is the old API. Its deprecated now, and would be removed soon. Please use the latest one which is as follows:

      bpmrulesruntime/rules-service/rest/v1/rule-services/java/ShoppingCartPromotionRules/DiscountRuleservice

      • You missed the highlighted part as shown above
      • Use the API Business Hub to know the details. You can also test the service there as well

      https://api.sap.com/shell/discover/contentpackage/SAPCPBusinessRulesAPIs/api/SAP_CP_BusinessRules_Runtime

      Regards,
      Archana

      (0) 
      1. Former Member

        Hi Archana,

        The “rules-service” part is defined in my neo-app.json:

        I do miss the “/java” part actually.

        Thanks for your help! It is able to work now.

        (0) 
  3. Former Member

    Hi Archana,

    Additionally, the return value — result1 can’t be read when it be used as result[0] in my project

    when I change it to result1, it can work normally.

    I have no idea why it happened.

    Could you please give some advice when you feel free?

    Best Regard,

    Joy Lee

    (0) 
    1. Archana Shukla
      Post author

      It depends upon what is the output of the rules. If you the output is single value then it is returned as { someoutput} but if the output is an array then it is returned as [{someoutput1},{someouput2}]

      So, I think your output is not an array and hence result1[0] wont wort instead it would be result1

      You can test the service via POSTMAN or API Business Hub and see the output. Based on that you code in your application.

      Regards,
      Archana

      (0) 
  4. Swetha Balusamy
    Hi Archana,
    Thanks for sharing the blog.

    I have a few questions in ERM.

    1. Can we create a data object of “table” type?.

    Currently it is giving option only to create data object of type “structure”.

    2. In BRF+ we have an option to call static method from BRF+ application.

    Do we have the same feature supported in ERM?.

     

    Thanks in advance,

    Swetha.

     

    (0) 
    1. Archana Shukla
      Post author

      Hello Swetha,

      • No currently you cannot have data object of type table. This will be available in Q2-2018 and with S/4 HANA 1809 release you would be able to deploy rules modelled in SAP Cloud Platform Business Rules to your backend S/4 HANA system.
      • No, calling static methods would not be available in SAP CP Business Rules

       

      (0) 
  5. Swetha Balusamy

    Hi  Archana,

    Currently I see expressions decision table and text rule.

    When will we get expressions like lookup,loop in ERM ?.

     

    Regards,

    Swetha.

     

     

    (0) 
  6. Ashok Kumar M

    Hi Archana Shukla ,

    From the Github link I downloaded ‘shoppingcart.zip’ (UI app) and ‘shoppingCartPromotionRules.zip’ (Rules project).

    I was able to deploy the UI app and but deployment of rule project resulted in errors.

    Activating at project level would activate Project, Data Objects, Rule Services, Rules, Rulesets or the entire hierarchy has to be activated individually?

    Best Regards,
    Ashok.

    (0) 
  7. Anurag Garg

     

    HI Archana,

     

    I am getting an error while deploying my rule service on cloud.

    Error: Unable to deploy rule service. When I opened chrome–> developer tools–> saw an HTTP error in Network tab -> Http status 403 forbidden.

     

    Please help what is going wrong?

    (0) 

Leave a Reply