Overview
This article is an update to xRM Formula #280, where the rules were executed via a Power Automate Flow. With updated functionality we can now call a Custom Action directly within a Power App, thereby simplifying the implementation and allowing significantly faster synchronous processing times.
Complex rules are often required to be executed as part of a Canvas Power App. North52's business rules engine for Microsoft Dynamics 365 and the Power Platform is a perfect partner when you need to evaluate complex rules in a Canvas App.
In this article, we will use the example of calculating a risk score for Coronary Heart Disease via a Canvas App. The rules and Decision Table structure for the calculation have been outlined in a previous article where the calculation is being made in a Model-Driven App.
This example also shows how stateless processing can be used with North52's business rules engine.
Rules for Calculating Coronary Heart Disease Risk
Please see the previous article for the rules table.
North52 Decision Suite Solution
The North52 Decision Suite solution works like this:
- A Custom Action is set up with one input and one output parameter
- The Custom Action is triggered from the Canvas App via the Environment object
- The Canvas App passes data from the App to the Custom Action
- A Formula is set up on the Custom Action which:
- Takes the data from the Canvas App
- Evaluates the data using risk criteria on several Decision Tables
- Returns results of the Risk Score as JSON
- The JSON is returned to the Canvas App and converted into a Collection
- The data from the Collection is used to display the Risk Score and Messages on the Canvas App
Set up Global Action, Formula, Flow and Canvas App
There are 3 parts to configure for this example: Global Action, Formula, and a Canvas App.
Global Action
Set up a Global Action with two parameters:
- PayloadInRiskParametersJSON
- Type = String
- Direction = Input
- PayloadOutRiskResultsJSON
- Type = String
- Direction = Output
Formula
For details of how to construct the evaluation criteria for the risk score see the xRM Formula #161 - Coronary Heart Disease Risk Score article.
The major differences for this example are:
- The Formula is of Type Action
- Source Entity is N52 Command
- JSON is parsed on the Global Calculations sheet
- These values are used on the Decision Sheets evaluating the risk
- The Risk Score and Messages are output to the Action parameter PayloadOutRiskResultsJSON
Formula Type
- The Formula is of Type Action bound to the Action Event n52demo_HeartDiseaseRiskAction
- First Decision Sheet calculating the Smoking risk is shown below:
Global Calculations Sheet
- The Global Calculations sheet maps values from the PayloadInRiskParametersJSON input parameter to individual calculations that can be easily referenced in the Decision Sheets (like the Smoking one above)
- Firstly the PayloadInRiskParametersJSON input parameter is assigned to the North52 internal variable called responsecontent
- Then individual values are assigned, for example, Current Smoker is assigned on row 4
Risk Calculation Sheet
- Column J is an incremental inline calculation
- Column K is an inline calculation that calls a parameterized Global Action (see next image below for details)
- The highlighted cell K4 shows the BuildJSON Global Action with the first parameter 'Smoke Risk' and the second parameter 'The patient has a smoke risk.'
BuildJSON Global Action
- The BuildJSON Global Action shows how we are constructing JSON to collate the messages for each matching risk
- An example of the output for this Global Action:
{ "risktype": "Smoke Risk", "message": "The patient has a smoke risk." }
Action Response Sheet
- This sheet provides the response to the Action's PayloadOutRiskResultsJSON output parameter
- The highlighted contents of cell A4 shows construction of the JSON, including the Risk Messages JSON from above
- An example of the output for this output parameter:
{ "Results": { "RiskScore": 3, "RiskMessages": [ { "risktype": "Smoke Risk", "message": "The patient has a smoke risk." }, { "risktype": "Dyslipidemia Risk", "message": "The patient has a dyslipidemia risk." }, { "risktype": "Family History Risk", "message": "The patient has a family history risk." } ] } }
Canvas App
Within the Canvas App there are two important parts to review:
- The OnSelect property of a button to collect the values and submit them to the Custom Action
- The DataTable which shows the messages that have been returned as part of the JSON response
OnSelect of Button
- The values from many input objects are collated in a Collection called FormulaData (1)
- This FormulaData collection is passed to the Environment.n52demo_HeartDiseaseRiskAction({PayloadInRiskParametersJSON:JSON(FormulaData)}).PayloadOutRiskResultsJSON (2)
- The Custom Action returns the results from the North52 calculation and adds this to a variable called PayloadOutRiskResultsJSON (2)
- The variable Score is set by parsing the JSON Set(Score, Value(ParseJSON(PayloadOutRiskResultsJSON).Results.RiskScore)); (3)
- The Risk Messages are added to a collection called RiskMessages by parsing the JSON (4)
DataTable
- A DataTable is shown using the RiskMessages Collection