Overview of Business Rules in Cloud Platform
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.
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
Hi Prudencia,
Thanks for your feedback. When you activate the Business Rule service, it should generate the destination. Can you please check again.
Thanks.
Wonderful Blog, Murali. I was able to follow your blog and execute a Business Rules Service on SCP. Thanks for Sharing 🙂
Thanks Prasanna
Concise article. Please keep making tutorials/documentations like this Murali, very much appreciated. Thanks!
Thanks Adrian
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.
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:
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.
Hi Archana,
When I use the Postman client to get the X-CSRF-Token through the API, it dosen't work. Here is the details:
image1
Could you please help me to solve the trouble?
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 -
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
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."
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
Hi Alejandro, Not sure what you are referring to. You need to set CreditLimitDO = Result and Customer = Input.
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
Hi Kavya, I think this should be feasible. In your case, your have 3 input parameters for your rules engine. Two of which would be provided via Workflow context and the third one is calculated at runtime. May be you can create an APIs to provide the calculated value and also invoke that during the workflow step.
Hi Murali,
Thank you for your clear and detailed post. I tried to improve on your scenario.
I have an issue sending to the Business Rule Engine an array one Data Objects in a relation 1..n with other Data Objects.
I got the following error: "error": "multiple generic objects found for CustomerSegment:get"
The idea is to have a Customer and his/her multiple CustomerSegment. I just pass a json array with one Customer and multiple CustomerSegment but it seems not working when there’s more than one Child object. I was not able to find an example as the documentation only shows how to pass on Employee with only one Address.
Thanks in advance
Mario
PS: JSON example
[{"__type__": "Customer", "CustomerId": "AII987u9", "WaitingTime": 3},
{"__type__": "CustomerSegment", "CustomerId": "AII987u9", "Segment": "Residential"},
{"__type__": "CustomerSegment", "CustomerId": "AII987u9", "Segment": "High Value"}
]
Hi Mario,
I am not sure if an array can be passed and I dont see it documented in the API description. I will check.
Hi Murali,
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.
Archana Shukla Is there a plan to support table type?
I have already replied to Swetha in another blog post where she posted the same question.
https://blogs.sap.com/2018/01/18/use-sap-cloud-platform-business-rules-in-your-custom-applications/
Hi,
I can invoke my rule service from the API Business Hub but when I try to invoke from postman I get the error 403 forbidden.
I can get the X-CSRF-Token, I set it into the header and then I post to:
https://bpmrulesruntimerules-XXXXXXX.hana.ondemand.com/rules-service/rest/v1/rule-services/java/SB_Test01/ProductPriceRule
User as role RuleSuperUser assigned.
Thanks for support
Hi Samuele,
Can you please raise this as a question in the forum. You can tag me so that I can try and find out where the problem is. Thanks
Hi Murali,
Solved.
I just run the postman saved request and I get “HTTP Status 415 – Unsupported Media Type” then I have set the Content-Type application/json and now I get no errors.
Thank you
Hi Murali, can we also use variables in the rules? for instance in your sample rules you wrote values like: is greater than 10000, is less than 2000.
But I need a rule like: if "salary" of "customer" is greater than "value" of "salaryAnnounce then ...", where "customer" and "salaryannuonce" can be objects and "salary" and "values" are their attributes that I defined in business rules
Hello Samin,
I know its bit late to reply, but still would want to address your query. Variables can be achieved now in business rules by using a data object of type Element. You can then create a simple rule with that element, and then reuse that rule wherever possible. You can follow my blog for more information.
Please post the query here in the Business Rules community where it could be addressed quicker
Has anyone noticed performance issues on a Rule with 400 entries in it? The Business Rules Service edit functionality appears to struggle for us
Hello Andrew,
This was fixed with recent updates. Please let us know if you still face the issues. Please post the query here in the Business Rules community where it could be addressed quicker
Hi Murali- The Blog is really good. I have one Question
Do we have any specific limit of entries for the BR rules values . If we have any limit please share help document.
Regards
Praveen K
Hello praveen,
there is size limit of 100 KB on the business rules overall. You can find the same in FSD.
Please post the query here in the Business Rules community where it could be addressed quicker