Skip to main content

Executing Rules from Apex or Lightning Web Components

Rule Execution is handled on the Salesforce side by an Apex class installed with the InRule for Salesforce App called the DecisionClient. The DecisionClient is what accepts the run rules requests from the Run Rules button and trigger requests to handle sending it along to the Rule Execution Service, but it also accepts calls from other Apex classes or Lightning Web Components (LWCs). This section will document how to run rules from your own Apex classes or LWCs by calling the DecisionClient, as well as detailing what manner of response it returns.

To run rules from another Apex class, simply invoke the executeRules method in the DecisionClient with the following call:

inrule.DecisionClient.executeRules(String eventType, String id, String objectType, String ruleSetName, Boolean useEntityPrefix, String ruleAppName, String ruleAppLabel, string entityImage, Boolean persistChanges)

Alternatively, setting up a call to the DecisionClient from a LWC requires setting up an action. First, you reference the DecisionClient as your LWC's controller in your .cmp file:

<aura:component controller="inrule.DecisionClient">

To create the run rules action to hit the DecisionClient in your component controller, new up your action with by setting it as below:

var action = component.get('c.executeRules');

From there, the action can be setup and enqueued as with any other action, with the following parameters to set:

action.setParams({
eventType: string
id: string
objectType: string
ruleSetName: string,
useEntityPrefix: boolean,
ruleAppName: string,
ruleAppLabel: string,
entityImage: string,
persistChanges: boolean
});

Regardless of whether you are calling from Apex or a LWC, the arguments will need to be defined with the following values:

Parameter NameDescription
Event Type (String)The event type for invoking rules. This will always need to be either "insert," "update" or "delete," depending on what your rule is doing
entityId (String)The unique Salesforce identifier for the root object in the request. This is a property available on all Salesforce objects and be accessed with: "entityName.Id"
objectType (String)The Salesforce object type of the root object in the request. For example, if running rules against an Account entity, this will need to be set to a string value of "Account".
ruleSetName (String)The name of an explicit Rule Set to call as the entry point for rule execution.
useEntityPrefix (Boolean)If set to true, the DecisionClient will append the entity label to the supplied rule set name. For example, if you pass in a ruleSetName of "DefaultRules" and set useEntityPrefix to true, the effective ruleSetName name would be "AccountDefaultRules"
ruleAppName (String) (Optional)Optional. Defining and passing a RuleAppName here allows you to override the default Rule App Name defined in your Custom Setting created during initial configuration for this specific button. If you do not wish to override your Custom Setting, pass null here.
ruleAppLabel (String) (Optional)Optional. Defining and passing a rule app label here allows you to override the rule app label configured on the execution service. If you do not wish to override your execution service's configuration, pass null here.
entityImage (String) (Optional)Optional. This allows you to pass in the serialized JSON string of an entity image as it exists at the time of calling the DecisionClient, which will have rules execute against the entity image passed in, not the entity image as it exists in Salesforce when the execution process reaches the execution service. If you do not wish to pass this in, pass null instead.
persistChanges (Boolean) (Optional)Optional. This allows you to define whether to persist data changes made during rule execution to Salesforce. Pass in null to default this value to true.

The DecisionClient will always return a JSON string; a serialized version of the DecisionClientResponse object. Once you have received it back as a string, you will need to deserialize it to access its properties.

The available properties on the DecisionClientResponse are:

Property NameDescription
IsSuccess (Boolean)Denotes whether or not rules successfully ran with no errors.
Notifications (List<NotificationMessage>)Provides a list of all notification message returned by rule execution. Each NotifactionMessage has 2 properties on it:

- Type (Integer): The notification type is an integer that maps to a Salesforce notification type. 0 maps to Informational, 1 to Success, 2 to Warning, and 3 to Error.
- Message (String): The notification text
Errors(List<ErrorMessage>)Provides a list of all errors encountered during rule execution. These differ from errors thrown by the rule application itself, which are instead added to the Notifications list as NotificationMessages of type Error. Errors added into the Errors list are strictly errors encountered during rule execution runtime.

Each ErrorMessage has 2 properties:

- Source (String): At what point during runtime the error was thrown
- Message (String): The error text