Executing Dynamics 365 Rules from JavaScript
While InRule provides the ability to easily run rules on demand with the Run Rules button and form events, you can also write your own JavaScript for more advanced scenarios and consume the functions provided in the included invokeCustomAction.js resource directly.
Overriding Rule Configuration Behavior with User Options and Custom JavaScript
The Run Rules ribbon button executes the InRule.executeRules() method without passing in any parameters. Calling the method in this way will use the configuration defined by the InRule Solution – Rule Configuration Form. However, if an implementer chooses to call the InRule.executeRules() method from within custom JavaScript code, a userOptions object can be passed in that will override the Rule Configuration.
The below JavaScript clip demonstrates how to load up a userOptions object with the values, and then pass those options on to inRule.executeRules(formContext, userOptions). 'ruleSetName' can be either a rule set or decision name. To pass parameters to the rule set or decision, use ruleSetParameters and provide a JSON string using the format below:
var formContext = executionContext.getFormContext();
var userOptions = {
persistChanges: true,
useDirtyEntity: false,
entityTypeName: formContext.data.entity.getEntityName(),
entityId: formContext.data.entity.getId(),
useEntityPrefix: false,
showConfirmation: true,
showStatus: true,
ruleAppName: "MyRuleApplication",
ruleSetName: "MyRuleSet",
ruleSetParameters: '{"parameter1Name":"parameter1Value", "parameter2Name":"parameter2Value"}'
};
inRule.executeRules(formContext, userOptions);