There are no secrets to success. It is the result of preparation, hard work, and learning from failure.

Monday, 23 March 2020

SAP Cloud Platform Business Rules- Part 3

Click here and follow my SAP UI5/FIORI snippets and information Page
SAP Cloud Platform Business Rules- Part 1
SAP Cloud Platform Business Rules- Part 2
In the Part-1 and Part-2, I have shown required configurations steps and BRM Rules project creation with all required components.In Part-3,I going to explain below point.Go through it and please let me know in case of any doubt/issue.

->How to use/call BRM project in SAPUI5/FIORI application

Advantages:
1. Without writing the code in the application, we are getting the offers below.
2. Easily we can change the offers without touching the UI5 application code.
3. Anyone can maintain BRM Rules project.
4. Technical knowledge is not required for BRM Rules project.
5. It is easy to develop.
6. It is easy to enhance.

Step 1- Create a SAPUI5 application and paste below snippet in neo-app.json         
     {
      "path": "/bpmrulesruntime/rules-service/v1/rules",
      "target": {
        "type": "destination",
        "name": "bpmrulesruntime",
        "entryPath": "/rules-service/v1/rules"
      },
      "description": "SAPUI5 Test Resources"
    }

According to the BRM Rules project, I have created UI5 view. Please have a look at below screenshots.

Application UI Screens 

There are three drop-downs in this UI screen.All are mandatory fields here.  
Select Card Type from first drop-down.

Select Brand from second drop-down.

Select Quantity from third drop-down.

Select all the fields and click on "check" button for knowing the best offer.Based on the selections you will get offer from BRM Rules.

I have selected these options.Please find below the screenshot.




I have got the offer based on my inputs.Please find below the screenshot.
Step 2- Paste below snippet in view.

<content>
<f:SimpleForm editable="true" layout="ResponsiveGridLayout" labelSpanXL="3" labelSpanL="4" labelSpanM="3" labelSpanS="12" adjustLabelSpan="false" emptySpanXL="4" emptySpanL="4" emptySpanM="4" emptySpanS="0" columnsXL="1" columnsL="1" columnsM="1" singleContainerFullSize="false">
<f:content>
<Label text="Card Type" required="true"/>
<ComboBox placeholder="Select Card Type" id="cardTypId">
<core:Item key="SBI" text="SBI"/>
<core:Item key="HDFC" text="HDFC"/>
<core:Item key="TATA" text="TATA"/>
<core:Item key="CITI" text="CITI"/>
<core:Item key="ICICI" text="ICICI"/>
<core:Item key="AMERICAN EXPRESS" text="AMERICAN EXPRESS"/>
<core:Item key="IDFC" text="IDFC"/>
<core:Item key="RBL" text="RBL"/>
<core:Item key="BAJAJ" text="BAJAJ"/>
<core:Item key="AXIS" text="AXIS"/>
</ComboBox>
<Label text="Brand" required="true"/>
<ComboBox placeholder="Select Brand" id="brandTypId">
<core:Item key="SONY" text="SONY"/>
<core:Item key="LG" text="LG"/>
<core:Item key="SAMSUNG" text="SAMSUNG"/>
<core:Item key="SPYKAR" text="SPYKAR"/>
<core:Item key="ARROW" text="ARROW"/>
<core:Item key="MUFTI" text="MUFTI"/>
<core:Item key="LEVIS" text="LEVIS"/>
<core:Item key="MOCHI" text="MOCHI"/>
<core:Item key="MATROLA" text="MATROLA"/>
<core:Item key="NIKE" text="NIKE"/>
</ComboBox>
<Label text="Quantity" required="true"/>
<ComboBox placeholder="Select Quantity" id="quanTypId">
<core:Item key="1" text="1"/>
<core:Item key="2" text="2"/>
<core:Item key="3" text="3"/>
<core:Item key="4" text="4"/>
<core:Item key="5" text="5"/>
<core:Item key="6" text="6"/>
<core:Item key="7" text="7"/>
<core:Item key="8" text="8"/>
<core:Item key="9" text="9"/>
<core:Item key="10" text="10"/>
<core:Item key="11" text="11"/>
<core:Item key="12" text="12"/>
<core:Item key="13" text="13"/>
<core:Item key="14" text="14"/>
<core:Item key="15" text="15"/>
</ComboBox>
</f:content>
</f:SimpleForm>
<FlexBox alignItems="Center" justifyContent="Center">
<items>
<Button text="Reset" type="Reject" class="sapUiSmallMarginEnd" press="handleRest"/>
<Button text="Check" type="Accept" press="handleCheck"/>
</items>
</FlexBox>
<ObjectIdentifier title="**Festival Offers on credit cards" class="marginClass"/>
</content>

Step 3- Paste below snippet in controller.

// When users click on 'CHECK' button then this method will fire....
handleCheck: function () {
jQuery.sap.require("sap.m.MessageBox");
var cardId = this.byId("cardTypId"),
brandId = this.byId("brandTypId"),
QAId = this.byId("quanTypId"),
that = this;
if (!cardId.getSelectedKey()) {
sap.m.MessageToast.show("Select Card Type");
return;
}
if (!brandId.getSelectedKey()) {
sap.m.MessageToast.show("Select Brand");
return;
}
if (!QAId.getSelectedKey()) {
sap.m.MessageToast.show("Select Quantity");
return;
}
//Input(Entity/Structure name in business rules)....
//CardType,BrandType,Quantity(Properties names in business rules)....
var payLoad = {
"__type__": "Input",
// Date type is string in business rules....
"CardType": cardId.getSelectedKey(), 
// Data type is string in business rules....
"BrandType": brandId.getSelectedKey(), 
// Date type is number in business rules....
"Quantity": Number(QAId.getSelectedKey()) 
};
// 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({
// DemoRules1(Project Name in business rules)....
// OfferRulesService(Service Name in business rules)....
url: "/bpmrulesruntime/rules-service/v1/rules/invoke?rule_service_name=DemoRules1::OfferRulesService",
method: "POST",
contentType: "application/json",
data: JSON.stringify(payLoad),
async: false,
headers: {
"X-CSRF-Token": token
},
success: function (result1, xhr1, data1) {
var dis = null,
cashBack = null,
title = "",
icon = null;
if (result1.__type__) {
dis = result1.Discount + "%";
cashBack = result1.Cashback + " INR";
title = "CONGRATULATIONS";
icon = sap.m.MessageBox.Icon.SUCCESS;
} else {
dis = "0%";
cashBack = "0 INR";
title = "INFORMATION";
icon = sap.m.MessageBox.Icon.INFORMATION;
}
sap.m.MessageBox.show("Discount = " + dis + "\n" + "CashBack = " + cashBack, {
icon: icon,
title: title,
actions: [sap.m.MessageBox.Action.OK],
onClose: function (oAction) {
that.handleRest();
}
});
}
});
}
});
},

// When users click on 'RESET' button then this method will fire....
handleRest: function () {
this.byId("cardTypId").setSelectedKey("");
this.byId("brandTypId").setSelectedKey("");
this.byId("quanTypId").setSelectedKey("");

}

Saturday, 21 March 2020

SAP Cloud Platform Business Rules- Part 2

Click here and follow my SAP UI5/FIORI snippets and information Page
SAP Cloud Platform Business Rules- Part 1
SAP Cloud Platform Business Rules- Part 3
In part-1,I explained all the configurations steps for Business rules.In part-2, I am going to explain below points.Go through it and please let me know in case of any doubt/issue.

->How to create Business rules project.
->How to create Data Objects(Entity).
->How to create attribute.
->How to create service and vocabulary.
->How to create decision tree.
->How to write conditions in decision tree.
->How to deploy service on cloud.
Step 1- Click on "Manage Rules project". I have highlighted below in the screenshot.
Step 2- You will get below screen.         
Step 3- Click on add(+) button to create the rules project.I have highlighted above in the screenshot. 
You will get below screen.
I have given input in Name filed is : "DemoRules1"
I have given input in Description filed is : "Offers for credit cards example"
You can give any name as per your requirement.
  A. Click on "Save" button to create the project.
  B. Click on "Validate" button to validate the project.
  C. Click on "Edit" button.
  D. Click on "Activate" button to activate the project.

Step 4- Select "Data Objects" and from the menu list, select the "Local data Objects".I have highlighted below in the screenshot.
Step 5- Click on Add(+) button to create entity. I have highlighted above in the screenshot.
Entity- It contains properties.
Give entity name, description and select type.In the select option you will get three options.Select the one option as per your requirement.
Options are:   A- Structure
                     B- Table
                     C- Element
I have selected structure for my case. After filling the details,click on save button to create the entity.
Click on edit button and select "Attributes" option and add at least one property under the entity.
 Add property under the attributes section and then save, validate and activate the entity.
 I have created three properties for my input case.
You have to create two entities, one for input and another one for output.
I have created two properties for my output case.follow same above steps for output entity and properties.
Step 6- Select "Rule Services" option and click on add(+) button to create service. I have highlighted below in the screenshot.
  
Provide the details and save to create service.
Click on edit button and select "vocabulary".
Click on add(+) button to add vocabulary under the service.
In vocabulary, we select created entities and it's usage(whether input or result). Save, Validate and Activate.

Step 7- Select "Rules" and from the menu,select "Local Rules".
Click on add(+) button to create rules. You will get below screen.
Give Name and create the rules.You will get below screen.
Click on setting button.I have highlighted above in the screenshot. You will get below screen.
 A. Change Hit policy to "All Match".
 B. In condition write your properties of created input entity.Suggestions would be coming automatically.
 C. Select operator and click on plus(+) button to add row below.
 D. Select output entity.All properties would be added under the result.
 E. Give default value 0.
 I have added three conditions because I have three properties in input entity.
 Click on Apply.You will get below screen.
Click on "Add Row" and select "Insert First".
Write value in input and output properties.If it is string data type then write with quote.If it is number data type then write without quote.
Save,Validate and Activate. You can add any number of row.

Step 8- Select "Rulesets" and click on add(+) button to create rulesets.

Give name, description and select rule service.
Click on Add rule and select created rule.
Save,Validate and Activate.

Step 9- Select "Rule Services" and click on the deploy button. I have highlighted below in the screenshot.

You will get below screen.
Click on "SAP Cloud Platform".You would get successful deployment message.

Congratulations! Successfully we have created BRM project,Entities,Attributes,service,vocabulary,decision tree with conditions and deployment of BRM on cloud .

In the next post I will show, How to use/call BRM project in SAPUI5/FIORI application.