Skip to Content

Business Rules is a service on SAP Cloud Platform which is currently available as beta in trial landscape. In this blog, I am going to show how easy it is build business rules on the platform and consume it in other applications.

What are Business Rules

For those who are new to the concept of Business Rules, it’s a flexible way to separate business logic from application code. The configuration of the rules can be handed over to Key users to manage it themselves. Typically, in the past, developers used to hard code business logic straight into the programs they code – for example Tax or discount calculations. Obviously, when there is a change in these calculations, the developers would need to be brought in to make the changes. Now, with Business Rules, it allows developers to configure the same outside the application and invoke them whenever necessary. These Business rules can be shared by different applications too. If you have heard about on-premise BRF+, HRF etc, Business Rules in the Cloud Platform performs a similar role.

Where do we use Business Rules

You can use Business Rules in almost any Cloud Application where you want a flexible way to handle business logic. The below are few scenarios where Business Rules can be applied.

  • Brand new applications in the Cloud Platform
  • Extension applications for Cloud Solutions like SuccessFactors, Ariba etc;
  • Workflow Scenarios

 

Prerequisites

Unlike few other service, Business Rules services doesn’t have any dependency with other services in the Cloud Platform. Once you subscribe to it, you can start configuring your rules and consume it straightaway.

To get started, follow this blog “SAP Cloud Platform Business Rules – Try it Yourself” by Archana Shukla, where she has detailed all the steps which includes enabling the service and assignment of relevant roles.

Configure Business Rules

To illustrate how Business Rules works, I have put my business logic in a spreadsheet. This spreadsheet is used to calculate the maximum credit limit allowed to a customer based on their credit rating and value of the order which they have placed. A customer with credit rating “A” with order value less than 100,000$ will get maximum credit limit of 10,000$.

 

When I launch the Business Rules service, I will be taken to the Projects section. This is where I would maintain several projects. For this example, I have created the project “CreditLimit”.

Within the projects, there are five sections – Details, Data Objects, Rule Services, Rule and Rulesets.

In the Details section, I have provided a name & description for the project.

In the Data Objects, I have created two objects – Customer and CreditLimitDO. Data Objects help form the rule vocabulary.

The Customer data object represents an input structure which will define the format in which data is passed to the business rule when it is invoked. In the below example, I have defined two attributes – Credit Rating and Average Order Value. These attributes need to be populated with an input value when invoking the business rule.

Before activating the data objects, I have set the Target runtime as “JAVA” and Target Runtime variant as “Cloud” for each attribute as well as in the Mappings section.

The “CreditLimitDO” is another data object which will represent the output of the business rule. It comprises of one attribute called “Limit”. This object is going to be used as the structure for the output response when the business rule is invoked. Similar to the above step, I have set the Target runtime as “JAVA” and Target Runtime variant as “Cloud” for the Limit attribute as well as in the Mappings section.

The next section which I have maintained is the “Rules”. This is where we would configure the business logic.

You will notice that there are two sections within the Rules – Details and Decision Table. In the Details section, I have provided a name for the Rule and identified the Result Data object.

In the Decision table, I have populated the business logic as shown below. This will represent the excel sheet which I maintained earlier. Notice that the third column “Limit” is automatically added once the Result Data object was set as “CreditLimitDO”. Save and activate the Rule.

The next section I have maintained in the “Rules Service”. The Rule Service provides an interface that can be invoked from other cloud applications.

In the execution context, I have provided which data object serves as the context and Result. In the below example, I have marked “Customer” as Context and “CreditLimitDO” as Result. In the Target runtime section, I have set the Target runtime as “JAVA” and Target Runtime variant as “Cloud”. Save and activate the Rule Service.

The last section which I have maintained in the “Rulesets”. A Ruleset can be used to group several rules which get evaluated based on the Rule Services.

In the below Ruleset example, I have added the Rule and Rule Service created in the earlier step.

With this I have completed configuring each of the objects required for the Business Rules. In order to deploy this rule, I have used the “Deploy” option which is available within the Rule Service.

APIs for Business Rules

We can now invoke the business rule using APIs which are exposed from the Cloud Platform. For demonstration, I am using a REST client to invoke the APIs.

The API documentation for Business Service is available on SAP help.

You can browse through the list of runtime APIs and view the documentation to understand the input and output of each of these APIs.

Invoking a business rule via APIs is a two-step process. The first step is to obtain a CSRF token from the Rules service. Provide the below values in the REST client.

URL: https://bpmrulesruntimebpm-<pXXXX>trial.hanatrial.ondemand.com/rules-service/v1/rules/xsrf-token

Operation: GET

Header: X-CSRF-Token = Fetch

Header: Authorization = Basic Authentication (provide your SAP Cloud Platform login)

Note: The portion of the URL which is highlighted in brown can be obtained from the “bpmrulesruntime” destination which would have been createdin your SAP Cloud Platform cockut when you activated the service.

Once you execute the API call, it will provide an X-CSRF-Token in the response header. Make a note of it as it will be required for the subsequent call.

To invoke the Business Rule, you would need to provide the Project name of the Business Rule along with the Rule Service name. Provide the below values in the REST Client

URL : https://bpmrulesruntimebpm-<pXXXX>trial.hanatrial.ondemand.com/rules-service/v1/rules/invoke?rule_service_name=<projectName>::<RuleService>

Operation: POST

Header: X-CSRF-Token = <value obtained in previous step>

Header: Content-Type = application/JSON

In the Body, I have provided the below JSON as input to the service. Notice that I have provided the input data object “customer” as value for “__type__” and also provided values for both the attributes.

[
{
"__type__":"Customer",
"CreditRating":"A",
"AvgOrderValue":200000
}
]

When I execute the API with the JSON payload, it would now return the “Limit” as the output.

If you scroll above and look at the Decision Table which was maintained earlier, you will notice that Customer with Credit rating “A” and an average order value of 200,000$ would get a credit limit of 30,000$.

I can now use these APIs in any Fiori Application or even invoke them from within the Workflow scenarios to easily apply business rules.

You can find more info on the Business Rules service on the SAP Help documentation.

To report this post you need to login first.

13 Comments

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

  1. Prudencia Zebaze

    Hi Murali, i thank you for this tutorial.

    I have configured the Business Rules as explained in the tutorial, however no destination was defined in my SAP Cloud Platform. Please do you know what could be the reason?

    Thanks in advance

    Prudencia

    (0) 
    1. Murali Shanmugham Post author

      Hi Prudencia,

      Thanks for your feedback. When you activate the Business Rule service, it should generate the destination. Can you please check again.

      Thanks.

      (0) 
  2. Peter Chezowitch

    Thank you for this introduction. I am currently trying to use rules for a demo case. My requirement would be that I have a date in the result set. Is it somehow possible to define a date target field based on today + x days? I have tried today + 5 for example, but it does not work.

     

    (0) 
  3. junyi li

    Hi Murail,

    Tks for your sharing, When I use the Postman client to get the X-CSRF-Token through the API, it dosen’t work. Here is the details:

    Cache-Control →private
    Cache-Control →no-cache, no-store, must-revalidate
    Connection →Keep-Alive
    Content-Encoding →gzip
    Content-Length →2087
    Content-Type →text/html;charset=utf-8
    Date →Fri, 05 Jan 2018 03:06:57 GMT
    Expires →Thu, 01 Jan 1970 00:00:00 UTC
    P3P →CP=”IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”
    Pragma →no-cache
    Server →SAP
    Strict-Transport-Security →max-age=31536000; includeSubDomains; preload
    Vary →Accept-Encoding
    com.sap.cloud.security.login →login-request
    set-cookie →oucrssceonvvwxxkvwzmijewo=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsdsvwtnytvyfinrxdmnph=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsmkwvxvnjkmjxiimetvzk=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsekjhdqcufjreufttsbtx=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsggljzcqsgrfrbfgomrgt=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsxavbkgmjzjbtxtmauodn=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrstltjmhjgehisgaztflzj=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsmvswxteifrrhafmgmhzr=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsxroxadzmazfayenecamb=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsdexslpxrlcipwnrgacqi=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrswnhilfcbwkakmmnxgrop=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrssiatugcyjjfwzvgtdcbp=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrstqziruorioptolrnimto=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsberwqdfmbmyqyexzrcyk=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsrgoaqpzoymxsjlwznazi=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrstlcrunhbpbnutwxicwed=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie →oucrsybcqjhbpxhrtpwfauykh=0; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    set-cookie→oucrseazuunqcilxwjwxpnxwy=iK5DjQZTbvi05RX616P%2FkVvqj0ipu4UWQ5fMAyIGXbCogps5Ubn5%2B7M8NfgRzKCvhdlWRyFHCCZNy%2BjCXDTQb2VsmoBsZUeu0uIun5TyoJUigoAO0VmL%2B08ne%2B%2FjxMFo%2F6AH1TbS0sWervw6ZzYP4o49sttrhbJgKqWX80Rbb9SkRtIlAue0Qg0LNLcZecVts3iLk44PgEa5qNMxVkeXa6S7wlnCk%2BuXddSYjYOi8OfS%2BAD9PaSWVtzuy1one%2Fk0WCiTOwjBt6eAPhmgW4ilD71kHEpZZrgLI4U8hrsXLx0JpFM6y645aWekvfVHlTKPsF5JbBObxWAc6C17tXkmXBKHpGMM52QDsgILutdjMX3QDGOeInhdsOq43b4MCLVj%2F1T1kcfLWAu9Wi2o6b67%2BR8kUmduf4Qx9gsROXT88FWZy4KBsptd%2BgPhxYf1EsXT; Domain=.hanatrial.ondemand.com; Path=/; Secure; HttpOnly
    Could you please help me to solve the trouble?
    Best Regards
    Junyi
    (0) 
    1. Archana Shukla

      Hello Junyi,

      Please check if you have RuleSuperUser role for Business Rules Runtime assigned to your user. Also cross check if the user id you are using are correct.

      (0) 
  4. Vippin Chhibbar

    Hi,

     

    I enabled the Business Rule service in the SAP Cloud Platform tenant and assigned the Super user roles to all Business Rules Runtime, Business Rules Repository and Business Rules Editior. I also noticed that the following destinations were also created successfully –

    bpmrulesdesigntime

    bpmrulesrepository

     

    bpmrulesruntime

    The desitnations are created with below default parameters:

    Name:
    bpmrulesdesigntime
    Type:
    HTTP
    Description:
    bpmrulesdesigntime
    URL:

    https://bpmrulesrepositoryrules-xxxxxx.xxxx.hana.ondemand.com/rules-service
    Proxy Type:

    Internet
    Authentication:

    AppToAppSSO
    Additional Properties

    New Property
    Use default JDK truststore

    We have also Enabled the Principal propagation – for the subaccount.

    Still when we perform the check destination, we get following error:

     

    Failure reason: “Could not check at the moment. Please try again later”

     

    Please Advise.

    Thanks,

    Vipin

     

     

    (0) 
    1. Piet Bastiaens

      Hi,

       

      I discovered the same issue.

      I looked into connection issues because when I try to deploy a rule service I get the following error

      “An unknown error has occurred. Contact technical support.”

       

      (0) 
  5. Alejandro Chinchilla Ayapán

    Hi, when I try to add the execution context, on the usage column I only have Input and Result options. I don’t find the context value. Do you know why?

    (Entrada = In)

    (Resultado = Result)

    thanks for your response.

    Regards,

    Alejandro

    (0) 
  6. Kavya Chilukuri

    Hi Murali,

     

    I will be integrating Business rules with an SCP Workflow application.

    I have a scenario wherein a set of agencies need to be allocated incoming tasks.

     

    There are 3 parameters that would play a role in deciding the allocation. Two of these parameters would come directly from the Workflow payload, whereas the 3rd parameter is the current workload of each agency.

     

    The plan is to maintain and calculate the existing workload by means of a DB table and an SQL stored procedure.

    Do you think this is the appropriate way to handle the scenario or would you recommend a better alternative.

    Thanks in advance!

     

    Regards,

    Kavya

     

    (0) 

Leave a Reply