-
Notifications
You must be signed in to change notification settings - Fork 460
PlatformEventRecipesTriggerHandler
Demonstrates how to construct a trigger handler for platform events
Group Trigger Recipes
See TriggerHandler
Inheritance
private triggerNew
List<Event_Recipes_Demo__e>
Inherited
TESTVISIBLE
protected context
TriggerContext
Constructor responsible for transfering Trigger.new into a class variable
public PlatformEventRecipesTriggerHandler()
This is an admittedly contrived example. The key to Platform Event's utility is their interoperabilty. This recipe demonstrate what happens when a Platform Event is committed. Platform Event Triggers fire regardless of how the event was created:
- You can create these events either through Apex, low code tools
- and external system integrations
public override void afterInsert()
void
Account acct = new Account(Name = 'Awesome Events Ltd.');
insert acct;
Event_Recipes_Demo__e evt = new Event_Recipes_Demo__e(AccountId__c = acct.id, Title__c='Updated website', Url__c = 'https://developer.salesforce.com');
Database.saveResults result = PlatformEventsRecipes.publishEvent(evt);
System.debug(result + [SELECT Name, Website FROM Account WHERE Id = :acct.id]);
Inherited
This is main brokering method that is called by the trigger. It's responsible for determining the proper context, and calling the correct method
public virtual void run()
void
AccountTriggerHandler.run();
Inherited
Allows developers to prevent trigger loops, or allow a limited number of them by setting the maximum number of times this trigger is called.
public void setMaxLoopCount(Integer max)
Name | Type | Description |
---|---|---|
max | Integer | A valid number (generally 1) of times you'd like |
to allow the trigger to run. |
void
In the context of a TriggerHandler
class:
this.setMaxLoopCount(5);
Inherited
Allows developers to turn off the max loop count
public void clearMaxLoopCount()
void
In the context of a TriggerHandler
class:
this.clearMaxLoopCount();
Inherited
Allows developers to conditionally bypass (disable) other triggers that also implement this triggerHandler
public static void bypass(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | Class name (String) of the trigger handler to bypass |
void
TriggerHandler.bypass('AccountTriggerHandler');
Inherited
Removes a given trigger handler class name from the list of bypassed trigger handlers.
public static void clearBypass(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | Handler class name to remove from the bypass list |
void
TriggerHandler.clearBypass('AccountTriggerHandler');
Inherited
Allows developers to check whether a given trigger handler class is currently bypassed.
public static Boolean isBypassed(String handlerName)
Name | Type | Description |
---|---|---|
handlerName | String | The name of the trigger handler class to check for |
Boolean
TriggerHandler.isBypassed('AccountTriggerHandler');
Inherited
removes all classes from the bypass list
public static void clearAllBypasses()
void
Triggerhandler.clearAllBypasses();
Inherited
TESTVISIBLE
increment the loop count
protected void addToLoopCount()
void
Throws: loop count exception if the max loop count is reached
Inherited
TESTVISIBLE
make sure this trigger should continue to run
protected Boolean validateRun()
Boolean
TriggerHandlerException: thrown when executing outside of a,[object Object],trigger
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
context methods
protected virtual void beforeInsert()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeUpdate()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void beforeDelete()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUpdate()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterDelete()
void
Inherited
TESTVISIBLE
SUPPRESSWARNINGS
Virtual method for the implementing class to override
protected virtual void afterUndelete()
void