Platform Event Design when Subscribers are Apex TriggersAny Platform Event Library or Best Practices out there?Unable to cover try catch for UserPackageLicense test classDocumentation Incorrect? Is it possible to cover/deploy triggers on Platform Events?Testmethod verification that @future published Platform EventCharset for Platform Event not UTF-8?Platform Event Subscription is in Suspended StatusApex Platform Events Subscriber not always triggeredPlatform Event throws RetryableException and then SFDC stops the trigger from consuming events for hoursView Platform Event External SubscribersError handling when publishing platform eventsPlatform Events from flow are processed one by one or in bulk?Platform event subscribers on Visualforce page not being called always when platform event is published
Why did the "Orks" never develop better firearms than Firelances and Handcannons?
Using Sed to add counter to keyword
Platform Event Design when Subscribers are Apex Triggers
Isn't "Dave's protocol" good if only the database, and not the code, is leaked?
Does taking on an assistant professor position prevent me from doing post-docs later?
Did Snape really give Umbridge a fake Veritaserum potion that Harry later pretended to drink?
Why did C++11 make std::string::data() add a null terminating character?
What is meaning of 4 letter abbreviations in Roman names like Titus Flavius T. f. T. n. Sabinus?
Speeding up thousands of string parses
Do I need to be legally qualified to install a Hive smart thermostat?
How to figure out layers of the atmosphere?
Was Wolfgang Unzicker the last Amateur GM?
How can power levels matter in a magic system that emphasizes control?
Why did moving the mouse cursor cause Windows 95 to run more quickly?
How to improve the size of cells in this table?
Is there an easy way to index by a binary vector / mask?
Why would a propellor have blades of different lengths?
Does the Defensive Duelist feat stack with the Warforged integrated protection?
What is meant by perfect, imperfect consonance and dissonance?
Should I cheat if the majority does it?
Is there a typical layout to blocking installed for backing in new construction framing?
Two queries on triangles, the sides of which have rational lengths
How can solar sailed ships be protected from space debris?
how to convert Timestring to seconds
Platform Event Design when Subscribers are Apex Triggers
Any Platform Event Library or Best Practices out there?Unable to cover try catch for UserPackageLicense test classDocumentation Incorrect? Is it possible to cover/deploy triggers on Platform Events?Testmethod verification that @future published Platform EventCharset for Platform Event not UTF-8?Platform Event Subscription is in Suspended StatusApex Platform Events Subscriber not always triggeredPlatform Event throws RetryableException and then SFDC stops the trigger from consuming events for hoursView Platform Event External SubscribersError handling when publishing platform eventsPlatform Events from flow are processed one by one or in bulk?Platform event subscribers on Visualforce page not being called always when platform event is published
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is a general design question on how to structure trigger files for platform events. Say I publish a platform event "lead created"
and I want to do several distinct things within the same Salesforce org that published the event. I know the way that Salesforce "subscribes" to this event is via a trigger on the pEvt. I'm thinking the design should be multiple trigger files (one for each "subscriber") when processing the event via apex instead of the "one trigger per object" rule with custom objects. The thinking is that each trigger file is actually a distinct subscriber to the event and so should be separated out appropriately. This also means logic would be written within the trigger, which should be fine since the only phase that is supported is 'after insert'. Most trigger frameworks take care of working with the different phases which doesn't apply to pEvts.
I'd like to know if this design makes sense and if anyone else has implemented a platform event design where the "subscribers" are actually within the same Salesforce org that published the event.
platform-event design-patterns
add a comment |
This is a general design question on how to structure trigger files for platform events. Say I publish a platform event "lead created"
and I want to do several distinct things within the same Salesforce org that published the event. I know the way that Salesforce "subscribes" to this event is via a trigger on the pEvt. I'm thinking the design should be multiple trigger files (one for each "subscriber") when processing the event via apex instead of the "one trigger per object" rule with custom objects. The thinking is that each trigger file is actually a distinct subscriber to the event and so should be separated out appropriately. This also means logic would be written within the trigger, which should be fine since the only phase that is supported is 'after insert'. Most trigger frameworks take care of working with the different phases which doesn't apply to pEvts.
I'd like to know if this design makes sense and if anyone else has implemented a platform event design where the "subscribers" are actually within the same Salesforce org that published the event.
platform-event design-patterns
add a comment |
This is a general design question on how to structure trigger files for platform events. Say I publish a platform event "lead created"
and I want to do several distinct things within the same Salesforce org that published the event. I know the way that Salesforce "subscribes" to this event is via a trigger on the pEvt. I'm thinking the design should be multiple trigger files (one for each "subscriber") when processing the event via apex instead of the "one trigger per object" rule with custom objects. The thinking is that each trigger file is actually a distinct subscriber to the event and so should be separated out appropriately. This also means logic would be written within the trigger, which should be fine since the only phase that is supported is 'after insert'. Most trigger frameworks take care of working with the different phases which doesn't apply to pEvts.
I'd like to know if this design makes sense and if anyone else has implemented a platform event design where the "subscribers" are actually within the same Salesforce org that published the event.
platform-event design-patterns
This is a general design question on how to structure trigger files for platform events. Say I publish a platform event "lead created"
and I want to do several distinct things within the same Salesforce org that published the event. I know the way that Salesforce "subscribes" to this event is via a trigger on the pEvt. I'm thinking the design should be multiple trigger files (one for each "subscriber") when processing the event via apex instead of the "one trigger per object" rule with custom objects. The thinking is that each trigger file is actually a distinct subscriber to the event and so should be separated out appropriately. This also means logic would be written within the trigger, which should be fine since the only phase that is supported is 'after insert'. Most trigger frameworks take care of working with the different phases which doesn't apply to pEvts.
I'd like to know if this design makes sense and if anyone else has implemented a platform event design where the "subscribers" are actually within the same Salesforce org that published the event.
platform-event design-patterns
platform-event design-patterns
asked 9 hours ago
willardwillard
6645 silver badges17 bronze badges
6645 silver badges17 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
This is interesting, I quickly wrote 2 triggers on a platform event. And when I fired them I could see 2 debug logs, saying both triggers were own transaction.
Both being own transaction means, they get own set of limits. Also as both are the async process you genuinely don't care about the order of execution.
I have made architecture where we split our process via events and subscribe via triggers, but never tried something like having 2 triggers on 1 event.
I believe you can do this when you wanna make a couple of complicated processes independent of each other. Having multiple subscribers to same event do makes sense in my opinion.
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
add a comment |
This is all rather subjective. However, some comments from my perspective:
For testability, without the need to actually insert data into the database (for traditional triggers) or actually publishing events on the event bus (for platform events), IMHO you should always put all the trigger code into a separately testable Apex class, simply calling that code as a one-liner trigger. This has multiple benefits, and these are my top 2:
1.a. This makes your tests run faster (no need to insert into the DB through either mechanism)
1.b. You can use a "factory method with dependency injection" approach (see this previous answer for a description of that pattern) to allow mocking out trigger behaviour in "integration type unit tests"
It makes logical sense to me, in terms of design best practice, to split out the subscribers - after all, order of execution is very unlikely to be relevant between the subscribers as you have implied. You do, however, want to check to see if there are any limits that might get in the way of this approach.
We have implemented an Apex-based subscriber for our own Platform Event in our managed package. The reason we did this was two-fold:
3.a. The Platform Events are processed in a separate and asynchronous manner, regardless of what happens in the initiating session
3.b. The trigger is executed as a different user (the Automated Process user) than the contextual user where the event was published.
We used the "factory method with dependency injection" approach, and unit tests for our event subscriber code that don't actually rely on the publishing of events.
add a comment |
In addition to the other answers:
Once you go the multiple triggers per xxx__e approach you need to consider race conditions and record locking in your design. There is no guarantee of which trigger will execute "first" and furthermore, Trigger t might get X records per transaction and Trigger u might get Y (X notequal Y) records per transaction. Both triggers can execute concurrently.
It may also complicate any error retry use cases where the xxx__e event on Trigger t passes whereas Trigger u fails.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f267928%2fplatform-event-design-when-subscribers-are-apex-triggers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is interesting, I quickly wrote 2 triggers on a platform event. And when I fired them I could see 2 debug logs, saying both triggers were own transaction.
Both being own transaction means, they get own set of limits. Also as both are the async process you genuinely don't care about the order of execution.
I have made architecture where we split our process via events and subscribe via triggers, but never tried something like having 2 triggers on 1 event.
I believe you can do this when you wanna make a couple of complicated processes independent of each other. Having multiple subscribers to same event do makes sense in my opinion.
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
add a comment |
This is interesting, I quickly wrote 2 triggers on a platform event. And when I fired them I could see 2 debug logs, saying both triggers were own transaction.
Both being own transaction means, they get own set of limits. Also as both are the async process you genuinely don't care about the order of execution.
I have made architecture where we split our process via events and subscribe via triggers, but never tried something like having 2 triggers on 1 event.
I believe you can do this when you wanna make a couple of complicated processes independent of each other. Having multiple subscribers to same event do makes sense in my opinion.
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
add a comment |
This is interesting, I quickly wrote 2 triggers on a platform event. And when I fired them I could see 2 debug logs, saying both triggers were own transaction.
Both being own transaction means, they get own set of limits. Also as both are the async process you genuinely don't care about the order of execution.
I have made architecture where we split our process via events and subscribe via triggers, but never tried something like having 2 triggers on 1 event.
I believe you can do this when you wanna make a couple of complicated processes independent of each other. Having multiple subscribers to same event do makes sense in my opinion.
This is interesting, I quickly wrote 2 triggers on a platform event. And when I fired them I could see 2 debug logs, saying both triggers were own transaction.
Both being own transaction means, they get own set of limits. Also as both are the async process you genuinely don't care about the order of execution.
I have made architecture where we split our process via events and subscribe via triggers, but never tried something like having 2 triggers on 1 event.
I believe you can do this when you wanna make a couple of complicated processes independent of each other. Having multiple subscribers to same event do makes sense in my opinion.
answered 8 hours ago
Pranay JaiswalPranay Jaiswal
21.5k5 gold badges33 silver badges69 bronze badges
21.5k5 gold badges33 silver badges69 bronze badges
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
add a comment |
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
2
2
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
if each trigger is its own execution context, that makes a great case to have each "subscriber" separated out into its own trigger.
– willard
6 hours ago
add a comment |
This is all rather subjective. However, some comments from my perspective:
For testability, without the need to actually insert data into the database (for traditional triggers) or actually publishing events on the event bus (for platform events), IMHO you should always put all the trigger code into a separately testable Apex class, simply calling that code as a one-liner trigger. This has multiple benefits, and these are my top 2:
1.a. This makes your tests run faster (no need to insert into the DB through either mechanism)
1.b. You can use a "factory method with dependency injection" approach (see this previous answer for a description of that pattern) to allow mocking out trigger behaviour in "integration type unit tests"
It makes logical sense to me, in terms of design best practice, to split out the subscribers - after all, order of execution is very unlikely to be relevant between the subscribers as you have implied. You do, however, want to check to see if there are any limits that might get in the way of this approach.
We have implemented an Apex-based subscriber for our own Platform Event in our managed package. The reason we did this was two-fold:
3.a. The Platform Events are processed in a separate and asynchronous manner, regardless of what happens in the initiating session
3.b. The trigger is executed as a different user (the Automated Process user) than the contextual user where the event was published.
We used the "factory method with dependency injection" approach, and unit tests for our event subscriber code that don't actually rely on the publishing of events.
add a comment |
This is all rather subjective. However, some comments from my perspective:
For testability, without the need to actually insert data into the database (for traditional triggers) or actually publishing events on the event bus (for platform events), IMHO you should always put all the trigger code into a separately testable Apex class, simply calling that code as a one-liner trigger. This has multiple benefits, and these are my top 2:
1.a. This makes your tests run faster (no need to insert into the DB through either mechanism)
1.b. You can use a "factory method with dependency injection" approach (see this previous answer for a description of that pattern) to allow mocking out trigger behaviour in "integration type unit tests"
It makes logical sense to me, in terms of design best practice, to split out the subscribers - after all, order of execution is very unlikely to be relevant between the subscribers as you have implied. You do, however, want to check to see if there are any limits that might get in the way of this approach.
We have implemented an Apex-based subscriber for our own Platform Event in our managed package. The reason we did this was two-fold:
3.a. The Platform Events are processed in a separate and asynchronous manner, regardless of what happens in the initiating session
3.b. The trigger is executed as a different user (the Automated Process user) than the contextual user where the event was published.
We used the "factory method with dependency injection" approach, and unit tests for our event subscriber code that don't actually rely on the publishing of events.
add a comment |
This is all rather subjective. However, some comments from my perspective:
For testability, without the need to actually insert data into the database (for traditional triggers) or actually publishing events on the event bus (for platform events), IMHO you should always put all the trigger code into a separately testable Apex class, simply calling that code as a one-liner trigger. This has multiple benefits, and these are my top 2:
1.a. This makes your tests run faster (no need to insert into the DB through either mechanism)
1.b. You can use a "factory method with dependency injection" approach (see this previous answer for a description of that pattern) to allow mocking out trigger behaviour in "integration type unit tests"
It makes logical sense to me, in terms of design best practice, to split out the subscribers - after all, order of execution is very unlikely to be relevant between the subscribers as you have implied. You do, however, want to check to see if there are any limits that might get in the way of this approach.
We have implemented an Apex-based subscriber for our own Platform Event in our managed package. The reason we did this was two-fold:
3.a. The Platform Events are processed in a separate and asynchronous manner, regardless of what happens in the initiating session
3.b. The trigger is executed as a different user (the Automated Process user) than the contextual user where the event was published.
We used the "factory method with dependency injection" approach, and unit tests for our event subscriber code that don't actually rely on the publishing of events.
This is all rather subjective. However, some comments from my perspective:
For testability, without the need to actually insert data into the database (for traditional triggers) or actually publishing events on the event bus (for platform events), IMHO you should always put all the trigger code into a separately testable Apex class, simply calling that code as a one-liner trigger. This has multiple benefits, and these are my top 2:
1.a. This makes your tests run faster (no need to insert into the DB through either mechanism)
1.b. You can use a "factory method with dependency injection" approach (see this previous answer for a description of that pattern) to allow mocking out trigger behaviour in "integration type unit tests"
It makes logical sense to me, in terms of design best practice, to split out the subscribers - after all, order of execution is very unlikely to be relevant between the subscribers as you have implied. You do, however, want to check to see if there are any limits that might get in the way of this approach.
We have implemented an Apex-based subscriber for our own Platform Event in our managed package. The reason we did this was two-fold:
3.a. The Platform Events are processed in a separate and asynchronous manner, regardless of what happens in the initiating session
3.b. The trigger is executed as a different user (the Automated Process user) than the contextual user where the event was published.
We used the "factory method with dependency injection" approach, and unit tests for our event subscriber code that don't actually rely on the publishing of events.
answered 8 hours ago
Phil WPhil W
1,9261 gold badge3 silver badges11 bronze badges
1,9261 gold badge3 silver badges11 bronze badges
add a comment |
add a comment |
In addition to the other answers:
Once you go the multiple triggers per xxx__e approach you need to consider race conditions and record locking in your design. There is no guarantee of which trigger will execute "first" and furthermore, Trigger t might get X records per transaction and Trigger u might get Y (X notequal Y) records per transaction. Both triggers can execute concurrently.
It may also complicate any error retry use cases where the xxx__e event on Trigger t passes whereas Trigger u fails.
add a comment |
In addition to the other answers:
Once you go the multiple triggers per xxx__e approach you need to consider race conditions and record locking in your design. There is no guarantee of which trigger will execute "first" and furthermore, Trigger t might get X records per transaction and Trigger u might get Y (X notequal Y) records per transaction. Both triggers can execute concurrently.
It may also complicate any error retry use cases where the xxx__e event on Trigger t passes whereas Trigger u fails.
add a comment |
In addition to the other answers:
Once you go the multiple triggers per xxx__e approach you need to consider race conditions and record locking in your design. There is no guarantee of which trigger will execute "first" and furthermore, Trigger t might get X records per transaction and Trigger u might get Y (X notequal Y) records per transaction. Both triggers can execute concurrently.
It may also complicate any error retry use cases where the xxx__e event on Trigger t passes whereas Trigger u fails.
In addition to the other answers:
Once you go the multiple triggers per xxx__e approach you need to consider race conditions and record locking in your design. There is no guarantee of which trigger will execute "first" and furthermore, Trigger t might get X records per transaction and Trigger u might get Y (X notequal Y) records per transaction. Both triggers can execute concurrently.
It may also complicate any error retry use cases where the xxx__e event on Trigger t passes whereas Trigger u fails.
answered 4 hours ago
cropredycropredy
37.8k4 gold badges45 silver badges132 bronze badges
37.8k4 gold badges45 silver badges132 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f267928%2fplatform-event-design-when-subscribers-are-apex-triggers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown