Use SAP Cloud Platform Business Rules in your custom applications
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 fromMurali Shanmugham 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:
- First you need to get the CSRF token while using the public API.
- 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:
- 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:
- content Type: is mandatory and have to be application/json
- 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
- 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
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
Hello Joy Lee,
The APIs that Murali used are older ones (which are no more valid), but the one I mentioned in my blogs are latest. You need to use the ones in my blog. You can see the APIs here from official documentation:
https://help.sap.com/viewer/p/BUSINESS_RULES
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
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
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 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
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
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
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 ?
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
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
https://api.sap.com/shell/discover/contentpackage/SAPCPBusinessRulesAPIs/api/SAP_CP_BusinessRules_Runtime
Regards,
Archana
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.
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
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
Got it, thanks for your help!
Best regards,
Joy Lee
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.
Hello Swetha,
Hi Archana,
Currently I see expressions decision table and text rule.
When will we get expressions like lookup,loop in ERM ?.
Regards,
Swetha.
Hello Swetha,
Value help will be available in Q4 2018. Loop are there in roadmap.
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.
Hello Ashok,
No. Activating at project level does not activate all the artefacts. You have to individually activate them from the editor. We will soon release activate all feature in editor.
You can also try to activate via API:
https://api.sap.com/api/SAP_CP_BusinessRules_Repository/resource
/projects/{id}/activation
Hope that helps,
Archana
Thanks Archana.
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?
Looks like you have not assigned RuleSuperUser role to your user for Business Rules Runtime.
https://help.sap.com/viewer/9d7cfeaba766433eaea8a29fdb8a688c/Cloud/en-US/f48a766c984644d2a49e72b5ca831ebe.html
Hellow Archana!
I’m in a big project for developing a hybrid app with sapui5 and i’m using the Business Rules Cloud.
Our customer raised some questions:
2. Can I configure a s-user or profile to see only the Rules tab?
3. Is there a key concept in the decision table? (Like a database) For exemple, this 2 lines do not make sense in first agreement configuration, and the Business Rules dont must be validate the decision table, but the table is validated:
Thank you!
Hello Sergio,
Here are answers to your query:
You can send me email if you have further queries pertaining to your project.
Regards,
Archana
Dear Archana,
We also have a use case in which we could use the effectivity dates. Any news on when this feature might become available ? I don't find information on this in the roadmap.
Kind Regards,
Igor.
Hi Archana,
I'm wondering, if these APIs would work for on premise BRF rules as well? Use case, could be that if we want to read the decision tables from a key user BADIs which allow only whitelisted APIs...
There are no public rest APIs for BRFPlus. These APIs are for SAP BTP Business Rules.
Hi Archana,
I want to invoke Business Rule from UI5 App which is built using Business Application Studio and deployed on
SAP BTP.
I have configured below code in UI5 App.
controller code
xs-app.json file
Destination created in SAP BTP
When I am invoking Business Rule from UI5 App through ajax call I am getting 404 not found error below is Error.
I have responded you in your Q&A.