Skip to content

PlatformEventRecipesTriggerHandler

pozil edited this page Nov 14, 2024 · 18 revisions

PlatformEventRecipesTriggerHandler Class

Demonstrates how to construct a trigger handler for platform events

Group Trigger Recipes

See TriggerHandler

Inheritance

TriggerHandler

Fields

triggerNew

Signature

private triggerNew

Type

List<Event_Recipes_Demo__e>


context

Inherited

TESTVISIBLE

Signature

protected context

Type

TriggerContext

Constructors

PlatformEventRecipesTriggerHandler()

Constructor responsible for transfering Trigger.new into a class variable

Signature

public PlatformEventRecipesTriggerHandler()

Methods

afterInsert()

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

Signature

public override void afterInsert()

Return Type

void

Example

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]);

run()

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

Signature

public virtual void run()

Return Type

void

Example

AccountTriggerHandler.run();

setMaxLoopCount(max)

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.

Signature

public void setMaxLoopCount(Integer max)

Parameters

Name Type Description
max Integer A valid number (generally 1) of times you'd like
to allow the trigger to run.

Return Type

void

Example

In the context of a TriggerHandler class:

this.setMaxLoopCount(5);

clearMaxLoopCount()

Inherited

Allows developers to turn off the max loop count

Signature

public void clearMaxLoopCount()

Return Type

void

Example

In the context of a TriggerHandler class:

this.clearMaxLoopCount();

bypass(handlerName)

Inherited

Allows developers to conditionally bypass (disable) other triggers that also implement this triggerHandler

Signature

public static void bypass(String handlerName)

Parameters

Name Type Description
handlerName String Class name (String) of the trigger handler to bypass

Return Type

void

Example

TriggerHandler.bypass('AccountTriggerHandler');

clearBypass(handlerName)

Inherited

Removes a given trigger handler class name from the list of bypassed trigger handlers.

Signature

public static void clearBypass(String handlerName)

Parameters

Name Type Description
handlerName String Handler class name to remove from the bypass list

Return Type

void

Example

TriggerHandler.clearBypass('AccountTriggerHandler');

isBypassed(handlerName)

Inherited

Allows developers to check whether a given trigger handler class is currently bypassed.

Signature

public static Boolean isBypassed(String handlerName)

Parameters

Name Type Description
handlerName String The name of the trigger handler class to check for

Return Type

Boolean

Example

TriggerHandler.isBypassed('AccountTriggerHandler');

clearAllBypasses()

Inherited

removes all classes from the bypass list

Signature

public static void clearAllBypasses()

Return Type

void

Example

Triggerhandler.clearAllBypasses();

addToLoopCount()

Inherited

TESTVISIBLE

increment the loop count

Signature

protected void addToLoopCount()

Return Type

void

Throws

Throws: loop count exception if the max loop count is reached


validateRun()

Inherited

TESTVISIBLE

make sure this trigger should continue to run

Signature

protected Boolean validateRun()

Return Type

Boolean

Throws

TriggerHandlerException: thrown when executing outside of a,[object Object],trigger


beforeInsert()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

context methods

Signature

protected virtual void beforeInsert()

Return Type

void


beforeUpdate()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void beforeUpdate()

Return Type

void


beforeDelete()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void beforeDelete()

Return Type

void


afterUpdate()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterUpdate()

Return Type

void


afterDelete()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterDelete()

Return Type

void


afterUndelete()

Inherited

TESTVISIBLE SUPPRESSWARNINGS

Virtual method for the implementing class to override

Signature

protected virtual void afterUndelete()

Return Type

void

Clone this wiki locally