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.

2 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) 

Leave a Reply