apex-trigger-actions-framework icon indicating copy to clipboard operation
apex-trigger-actions-framework copied to clipboard

Entry Criteria in Trigger Action Metadata

Open mitchspano opened this issue 1 year ago • 2 comments
trafficstars

With the introduction of the FormulaEval namespace (available in developer preview for Spring 24 release), Salesforce will enable us to define formulas as strings and dynamically express values with these formulas.

The Trigger Actions metadata within the framework should support these formulas to define an optional entry criteria for the actions.

global class TriggerActionRecord {
  global SObject record; // new version of the record in the DML operation
  gloabl SObject recordPrior; // old version of the record in the DML operation
}

For example, if we wanted a trigger action to execute only if the Description has been changed and now contains the string "Now take action", then we would define the entry criteria as such: Trigger_Action__mdt.Entry_Criteria__c

CONTAINS(record.Description, "Now take action") && (record.Description <> recordPrior.Description)

When the DML operation occurs, the system will create instances of TriggerActionRecord from the contents of Trigger.new and Trigger.old, then check them against the criteria formula.

If the formula evaluates to true, then the records will be filtered into the newList and oldList which will be passed into the Trigger Action class.

If no records evaluate to true, there will be no dynamic instantiation of the trigger action.

mitchspano avatar Jan 13 '24 19:01 mitchspano

Yes, I had a similar thought, so seeing it being considered is nice.

Question: what would be the benefit of adding an Entry Criteria on metadata vs a Short-Circuit condition at the start of the trigger handler method?

jefersonchaves avatar Mar 06 '24 10:03 jefersonchaves

The main advantage would be visibility of entry criteria from the metadata record itself without having to open the apex class. The second benefit would be to help with the trigger action flow recursion depth issue that is explained here:

https://github.com/mitchspano/apex-trigger-actions-framework?tab=readme-ov-file#define-a-flow

mitchspano avatar Mar 09 '24 18:03 mitchspano

This would also enable us to execute certain actions only for select Record Types, which I would greatly appreciate!

dschibster avatar Jul 25 '24 15:07 dschibster