Trigger Actions Framework : Amit Chaudhary

Trigger Actions Framework
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post


Administrators use platform products like Flow to deliver powerful business processes, and developers can write Apex to handle more complicated scenarios. The biggest question for Salesforce architects has been how to arrange these tools together to build complete solutions. In this post we will talk about how to orchestrate ALL of your Salesforce Automation with the Trigger Actions Framework.

Automation Choices can be Tricky…

It is a well documented best practice to pick one automation tool per sObject and stick with it. This is great in theory, but in practice it is rarely ever executed well. Many Salesforce orgs have a mixed solution of some logic in Apex triggers, some logic in Process Builder, and some logic in Flow for a given sObject. All of this automation can make maintaining and extending your Salesforce implementation very tricky and expensive in the long run. The Trigger Actions Framework offers a uniquely powerful way for developers and administrators to define and orchestrate automations on the Salesforce platform.

Partition your Logic and Control the Order

The Trigger Actions Framework framework allows developers to partition their trigger logic into small individual classes for each action they want to take, rather than combining all of those into one massive trigger handler class. After your action classes are written, use custom metadata to control the order of execution within a given context.

Trigger OpportunityTrigger on Opportunity (
  before insert, 
  after insert,
  before update,
  after update,
  before delete,
  after delete,
  after undelete
) {
   new MetadataTriggerHandler().run();
}

.

public class ta_Opportunity_StageInsertRules implements TriggerAction.BeforeInsert {
  @TestVisible
  private static final String PROSPECTING = 'Prospecting';
  @TestVisible

  private static final String INVALID_STAGE_INSERT_ERROR = 'The Stage must be \'Prospecting\'
when an Opportunity is created';
  public void beforeInsert(List<Opportunity> newList){
    for (Opportunity opp : newList) {
     if (opp.StageName != PROSPECTING) {
       opp.addError(INVALID_STAGE_INSERT_ERROR);
     }
    }
  }
}

Support for Flows

The Trigger Actions Framework can also allow you to invoke a Flow by name, and determine the order of the Flow’s execution amongst other trigger actions in a given trigger context. The framework has some features which allow you to check for a record’s previous value and apply your updates back to a record before insert/update. Here is an example of an auto-launched flow that checks if a records’ status has changed and if so, it sets the records description to a default value – all of this is done before update.

Here is an example of an auto-launched flow that checks if a records’ status has changed and if so, it sets the records description to a default value – all of this is done before update.

Flows are configured just like Apex triggered actions – just provide the API name of the Flow and the order in which you would like it to execute for the given context.

The Trigger Actions Framework can allow developers and administrators to create new features together while giving management and future team members a single view to understand all record automation on a given sObject. Be sure to check out the project on GitHub and share this with the developers, administrators, and architects in charge of your organization’s Salesforce implementation.

Recording

Thanks Mitch Spano for writing a guest blog post and sharing with us.

The post Trigger Actions Framework appeared first on Apex Hours.


April 30, 2021 at 07:28PM
Click here for more details...

=============================
The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce