Views:

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.

North52 business rules engine executing on Canvas Power App

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:

  • 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

North52 Business Rules Engine for Microsoft Dynamics - Custom Action

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:

Decision Table - North52 business rules engine for Microsoft Dynamics 365

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 

Decision Table - North52 business rules engine for Microsoft Dynamics 365

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.'  

Decision Table - North52 business rules engine for Microsoft Dynamics 365

BuildJSON Global Action

  • The BuildJSON Global Action shows how we are constructing JSON to collate the messages for each matching risk

Decision Table - North52 business rules engine for Microsoft Dynamics 365

  • 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

Decision Table - North52 business rules engine for Microsoft Dynamics 365

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

North52 business rules engine custom action canvas app

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

North52 business rules engines custom action canvas app

DataTable

  • A DataTable is shown using the RiskMessages Collection 

North52 business rules engines custom action canvas power app

Did you know?

There are a number of different ways North52 business rules can be used in Dynamics 365

These include:

  • Via API from another application. This allows a customized application to separate its rules so that they can be maintained outside of custom code.
  • In processes that loop over a collection of records - on Schedule or in real-time.
  • As a step in a Workflow, Custom Action, Business Process Flow or a Power Automate Flow.
  • Making forms more interactive. Visibility, validation, real time data updating is all provided by the North52 rule engine.
  • Security functions to restrict or open access to records.
  • ...and many more!

Learn more about North52 Business Process Activities