SlideShare a Scribd company logo
1 of 19
Platform Events
Salesforce’s Event-Driven Software Architecture
timdt01@gmail.com
Tim Taylor, Jacksonville Developer Group member.
What is an Event-Driven Software Architecture?
Event-Driven Software Architecture
● Consists of event producers, event consumers, and channels.
● Suitable for large distributed systems
● Decouples event producers from the event consumers.
● Components:
● Event: change in state meaningful to business
● Event Message: data about the event
● Producer: publisher of the event message over channel
● Channel: conduit to transmit the event
● Consumer: subscribes to a channel to receive event.
Custom Platform Events
● Platform events are messages that are
published and subscribed to.
● One or more subscribers can listen to
the same event and carry out
independent actions.
● This way, key components of an
integration are decoupled.
● Define a custom event in same way as
custom objects.
● Create the event by giving it a name and
adding custom fields.
Publishing Platform Events
● You can publish platform events by:
● Apex code.
● Point and click tools such as Process Builder and Flow Builder.
● Any Salesforce API from external applications.
● SOAP, REST or Bulk API
Platform Event
External
App
Salesforce
Trigger
REST API APEX
Apex Published Events
● To publish event messages by Apex, call the EventBus.publish() method passing an
instance of the custom event.
Process Builder Published Events
● Add a Create a Record action.
● For the Record Type, select the
custom platform event.
● Populate the fields of the platform
event.
API Published Events
● External applications use an
API to publish event
messages.
● You can use any Salesforce
API to create events in same
way as you would insert
sObjects.
● To publish multiple event
messages use the
composite resource.
REST endpoint:
/services/data/v47.0/sobjects/Registration_Notification__e
Request body:
{
"CaseId__c":"5008A000006GvpKQAS"
}
REST response:
{
"id" : "e01xx0000000001AAA",
"success" : true,
"errors" : [ {
"statusCode" : "OPERATION_ENQUEUED",
"message" : "154dba26-db92-4557-ad6b-9c7222428346",
"fields" : [ ]
} ]
}
Subscribing to Platform Events
● You can subscribe to platform events by:
● Apex Triggers.
● Point and click tools such as Process Builder and Flow Builder.
● Lightning apps can subscribe through the empApi component.
● Through CometD in external apps.
Platform Event
CometD
Apex Trigger
Process/Flow
Subscribing to Events with Apex Triggers
● Triggers provide an auto subscription mechanism in Apex. No need to explicitly create
and listen to a channel.
● Platform events ONLY support after insert triggers.
Subscribing to Events with Processes
● Processes also provide an auto
subscription mechanism for
published events. No need to
explicitly create and listen to a
channel.
● To subscribe to a platform
event, build the process to
start when it receives an event
message.
● Then associate the process
with a platform event and an
object
Subscribing to Events with Lightning Components
● Subscribe to platform events with
empApi component
● In Lightning Web components import
the methods from the lightning/empApi
module and then call the imported
methods from your javascript code.
● In Aura components add the
lightning:empApi component in your
markup and then add functions to call
the component methods in your client-
side controller.
Lightning Web Component:
Import { subscribe, unsubscribe, onError, }
from ‘lightning/empApi’;
Lightning Aura Component:
<lightning:empApi aura:id=“empApi” />
Subscribing to Events with CometD
● External systems can subscribe to
platform events using CometD
● The entities that consume platform
events should subscribe to the event’s
channel.
The platform event for our example:
/event/Registration_Notification__e
Event message when received:
{
"data": {
"schema":"dffQ2QLzDNHqwB8_sHMxdA",
"payload": {
"CreatedDate": "2020-03-09T18:31:40.517Z",
"CreatedById": "005D0000001cSZs",
"CaseId__c":"5008A000006GvpKQAS"
},
"event": {
"replayId": 1
}
},
"channel": "/event/Registration_Notification__e“
}
Automatic Retry Capabilities When Errors Occur
● Helpful to retry consuming an event when a transient error occurs.
● This is only available with event triggers.
● Retry events by throwing the EventBus.RetryableException.
Resuming a Platform Event
● If a trigger stops execution intentionally or because of unhandled exception
● Set a checkpoint to resume by setting replay ID of last successful event message.
● EventBus.TriggerContext.currentContext().setResumeCheckpoint(event.replayId)
Business Need – Organization Registration Notification
Salesforce is integrated with JIRA to allow for synchronization of JIRA tickets with the corresponding
Salesforce cases.
Organizations register with Availity daily. Some of those registrations are flagged for follow up for additional
information and a JIRA ticket is created along with a corresponding Salesforce case.
Our Fraud Analysts, working in JIRA, identify the necessary information needed and apply a series of labels
to the JIRA ticket and update the ticket status to Awaiting Reply. The Salesforce case is automatically
updated with this information.
The client support organization then manually reviews those labels and sends an email to the customer by
concatenating a series of Lightning Email Templates that correspond to the labels assigned to the case.
There are dozens of these registrations daily that often pile up for processing.
The Goal is to remove the manual process of sending the notifications to customers.
Apex Class
Solution – Organization Registration Notification
Platform Event
Process
Publishes Event
JIRA
Ticket
Salesforce
Case
Apex Trigger
REST API
Trigger Subscribes to
Event
“DEMO TIME!”
hope it works!
What’s Next?
How can I learn more about Platform Events?
Platform Events Developer Guide
https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm
Platform Events Basics
https://trailhead.salesforce.com/en/content/learn/modules/platform_events_basics
Platform Events by Tim Taylor

More Related Content

What's hot

What's hot (20)

Deep-Dive to Application Insights
Deep-Dive to Application Insights Deep-Dive to Application Insights
Deep-Dive to Application Insights
 
Salesforce integration best practices columbus meetup
Salesforce integration best practices   columbus meetupSalesforce integration best practices   columbus meetup
Salesforce integration best practices columbus meetup
 
Introduction to the Salesforce Security Model
Introduction to the Salesforce Security ModelIntroduction to the Salesforce Security Model
Introduction to the Salesforce Security Model
 
Salesforce Integration Pattern Overview
Salesforce Integration Pattern OverviewSalesforce Integration Pattern Overview
Salesforce Integration Pattern Overview
 
Intro to Force.com Canvas: Running External Apps within the Salesforce UI Web...
Intro to Force.com Canvas: Running External Apps within the Salesforce UI Web...Intro to Force.com Canvas: Running External Apps within the Salesforce UI Web...
Intro to Force.com Canvas: Running External Apps within the Salesforce UI Web...
 
Planning Your Migration to the Lightning Experience
Planning Your Migration to the Lightning ExperiencePlanning Your Migration to the Lightning Experience
Planning Your Migration to the Lightning Experience
 
Understanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We DoUnderstanding the Salesforce Architecture: How We Do the Magic We Do
Understanding the Salesforce Architecture: How We Do the Magic We Do
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patterns
 
Getting started with Salesforce security
Getting started with Salesforce securityGetting started with Salesforce security
Getting started with Salesforce security
 
Release and Enviromental Management
Release and Enviromental ManagementRelease and Enviromental Management
Release and Enviromental Management
 
OAuth with Salesforce - Demystified
OAuth with Salesforce - DemystifiedOAuth with Salesforce - Demystified
OAuth with Salesforce - Demystified
 
Security and Your Salesforce Org
Security and Your Salesforce OrgSecurity and Your Salesforce Org
Security and Your Salesforce Org
 
Best practices for implementing CI/CD on Salesforce
Best practices for implementing CI/CD on SalesforceBest practices for implementing CI/CD on Salesforce
Best practices for implementing CI/CD on Salesforce
 
Integrating with salesforce
Integrating with salesforceIntegrating with salesforce
Integrating with salesforce
 
Two-Way Integration with Writable External Objects
Two-Way Integration with Writable External ObjectsTwo-Way Integration with Writable External Objects
Two-Way Integration with Writable External Objects
 
Salesforce Integration
Salesforce IntegrationSalesforce Integration
Salesforce Integration
 
Event Mesh: The architecture layer that will power your digital transformation
Event Mesh: The architecture layer that will power your digital transformationEvent Mesh: The architecture layer that will power your digital transformation
Event Mesh: The architecture layer that will power your digital transformation
 
Build Reliable Asynchronous Code with Queueable Apex
Build Reliable Asynchronous Code with Queueable ApexBuild Reliable Asynchronous Code with Queueable Apex
Build Reliable Asynchronous Code with Queueable Apex
 
Einstein Next Best Action を試してみよう
Einstein Next Best Action を試してみようEinstein Next Best Action を試してみよう
Einstein Next Best Action を試してみよう
 
Setting up Security in Your Salesforce Instance
Setting up Security in Your Salesforce InstanceSetting up Security in Your Salesforce Instance
Setting up Security in Your Salesforce Instance
 

Similar to Platform Events by Tim Taylor

Understanding Business APIs through statistics
Understanding Business APIs through statisticsUnderstanding Business APIs through statistics
Understanding Business APIs through statistics
WSO2
 

Similar to Platform Events by Tim Taylor (20)

Apex Replay Debugger and Salesforce Platform Events.pptx
Apex Replay Debugger and Salesforce Platform Events.pptxApex Replay Debugger and Salesforce Platform Events.pptx
Apex Replay Debugger and Salesforce Platform Events.pptx
 
Platform events
Platform eventsPlatform events
Platform events
 
PlatForm Event.pptx
PlatForm Event.pptxPlatForm Event.pptx
PlatForm Event.pptx
 
ISV Monthly Tech Enablement (August 2017)
ISV Monthly Tech Enablement (August 2017)ISV Monthly Tech Enablement (August 2017)
ISV Monthly Tech Enablement (August 2017)
 
FIWARE Complex Event Processing
FIWARE Complex Event ProcessingFIWARE Complex Event Processing
FIWARE Complex Event Processing
 
FIWARE Complex Event Processing
FIWARE Complex Event ProcessingFIWARE Complex Event Processing
FIWARE Complex Event Processing
 
Event-Based API Patterns and Practices
Event-Based API Patterns and PracticesEvent-Based API Patterns and Practices
Event-Based API Patterns and Practices
 
Microsoft Graph API with OutSystems Event Subscriptions
Microsoft Graph API with OutSystems Event SubscriptionsMicrosoft Graph API with OutSystems Event Subscriptions
Microsoft Graph API with OutSystems Event Subscriptions
 
SAP workflow events
SAP workflow eventsSAP workflow events
SAP workflow events
 
Extending WSO2 Analytics Platform
Extending WSO2 Analytics PlatformExtending WSO2 Analytics Platform
Extending WSO2 Analytics Platform
 
[Webinar] Camunda Optimize Release 3.0
[Webinar] Camunda Optimize Release 3.0[Webinar] Camunda Optimize Release 3.0
[Webinar] Camunda Optimize Release 3.0
 
Replicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data CaptureReplicate Salesforce Data in Real Time with Change Data Capture
Replicate Salesforce Data in Real Time with Change Data Capture
 
ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017
 
TrailheaDX 2019 : Explore New Frontiers with High Volume Platform Events
TrailheaDX 2019 :  Explore New Frontiers with High Volume Platform EventsTrailheaDX 2019 :  Explore New Frontiers with High Volume Platform Events
TrailheaDX 2019 : Explore New Frontiers with High Volume Platform Events
 
Bloomberg API Open Source Development and Solution Providers India
Bloomberg API Open Source Development and Solution Providers IndiaBloomberg API Open Source Development and Solution Providers India
Bloomberg API Open Source Development and Solution Providers India
 
Publish and subscribe platform events using flows
Publish and subscribe platform events using flowsPublish and subscribe platform events using flows
Publish and subscribe platform events using flows
 
Platform Events Demo (1).pptx
Platform Events Demo (1).pptxPlatform Events Demo (1).pptx
Platform Events Demo (1).pptx
 
Understanding Business APIs through statistics
Understanding Business APIs through statisticsUnderstanding Business APIs through statistics
Understanding Business APIs through statistics
 
4.out port
4.out port4.out port
4.out port
 
Event Mesh Presentation at Gartner AADI Mumbai
Event Mesh Presentation at Gartner AADI MumbaiEvent Mesh Presentation at Gartner AADI Mumbai
Event Mesh Presentation at Gartner AADI Mumbai
 

Recently uploaded

Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital Businesses
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 

Platform Events by Tim Taylor

  • 1. Platform Events Salesforce’s Event-Driven Software Architecture timdt01@gmail.com Tim Taylor, Jacksonville Developer Group member.
  • 2. What is an Event-Driven Software Architecture? Event-Driven Software Architecture ● Consists of event producers, event consumers, and channels. ● Suitable for large distributed systems ● Decouples event producers from the event consumers. ● Components: ● Event: change in state meaningful to business ● Event Message: data about the event ● Producer: publisher of the event message over channel ● Channel: conduit to transmit the event ● Consumer: subscribes to a channel to receive event.
  • 3. Custom Platform Events ● Platform events are messages that are published and subscribed to. ● One or more subscribers can listen to the same event and carry out independent actions. ● This way, key components of an integration are decoupled. ● Define a custom event in same way as custom objects. ● Create the event by giving it a name and adding custom fields.
  • 4. Publishing Platform Events ● You can publish platform events by: ● Apex code. ● Point and click tools such as Process Builder and Flow Builder. ● Any Salesforce API from external applications. ● SOAP, REST or Bulk API Platform Event External App Salesforce Trigger REST API APEX
  • 5. Apex Published Events ● To publish event messages by Apex, call the EventBus.publish() method passing an instance of the custom event.
  • 6. Process Builder Published Events ● Add a Create a Record action. ● For the Record Type, select the custom platform event. ● Populate the fields of the platform event.
  • 7. API Published Events ● External applications use an API to publish event messages. ● You can use any Salesforce API to create events in same way as you would insert sObjects. ● To publish multiple event messages use the composite resource. REST endpoint: /services/data/v47.0/sobjects/Registration_Notification__e Request body: { "CaseId__c":"5008A000006GvpKQAS" } REST response: { "id" : "e01xx0000000001AAA", "success" : true, "errors" : [ { "statusCode" : "OPERATION_ENQUEUED", "message" : "154dba26-db92-4557-ad6b-9c7222428346", "fields" : [ ] } ] }
  • 8. Subscribing to Platform Events ● You can subscribe to platform events by: ● Apex Triggers. ● Point and click tools such as Process Builder and Flow Builder. ● Lightning apps can subscribe through the empApi component. ● Through CometD in external apps. Platform Event CometD Apex Trigger Process/Flow
  • 9. Subscribing to Events with Apex Triggers ● Triggers provide an auto subscription mechanism in Apex. No need to explicitly create and listen to a channel. ● Platform events ONLY support after insert triggers.
  • 10. Subscribing to Events with Processes ● Processes also provide an auto subscription mechanism for published events. No need to explicitly create and listen to a channel. ● To subscribe to a platform event, build the process to start when it receives an event message. ● Then associate the process with a platform event and an object
  • 11. Subscribing to Events with Lightning Components ● Subscribe to platform events with empApi component ● In Lightning Web components import the methods from the lightning/empApi module and then call the imported methods from your javascript code. ● In Aura components add the lightning:empApi component in your markup and then add functions to call the component methods in your client- side controller. Lightning Web Component: Import { subscribe, unsubscribe, onError, } from ‘lightning/empApi’; Lightning Aura Component: <lightning:empApi aura:id=“empApi” />
  • 12. Subscribing to Events with CometD ● External systems can subscribe to platform events using CometD ● The entities that consume platform events should subscribe to the event’s channel. The platform event for our example: /event/Registration_Notification__e Event message when received: { "data": { "schema":"dffQ2QLzDNHqwB8_sHMxdA", "payload": { "CreatedDate": "2020-03-09T18:31:40.517Z", "CreatedById": "005D0000001cSZs", "CaseId__c":"5008A000006GvpKQAS" }, "event": { "replayId": 1 } }, "channel": "/event/Registration_Notification__e“ }
  • 13. Automatic Retry Capabilities When Errors Occur ● Helpful to retry consuming an event when a transient error occurs. ● This is only available with event triggers. ● Retry events by throwing the EventBus.RetryableException.
  • 14. Resuming a Platform Event ● If a trigger stops execution intentionally or because of unhandled exception ● Set a checkpoint to resume by setting replay ID of last successful event message. ● EventBus.TriggerContext.currentContext().setResumeCheckpoint(event.replayId)
  • 15. Business Need – Organization Registration Notification Salesforce is integrated with JIRA to allow for synchronization of JIRA tickets with the corresponding Salesforce cases. Organizations register with Availity daily. Some of those registrations are flagged for follow up for additional information and a JIRA ticket is created along with a corresponding Salesforce case. Our Fraud Analysts, working in JIRA, identify the necessary information needed and apply a series of labels to the JIRA ticket and update the ticket status to Awaiting Reply. The Salesforce case is automatically updated with this information. The client support organization then manually reviews those labels and sends an email to the customer by concatenating a series of Lightning Email Templates that correspond to the labels assigned to the case. There are dozens of these registrations daily that often pile up for processing. The Goal is to remove the manual process of sending the notifications to customers.
  • 16. Apex Class Solution – Organization Registration Notification Platform Event Process Publishes Event JIRA Ticket Salesforce Case Apex Trigger REST API Trigger Subscribes to Event
  • 18. What’s Next? How can I learn more about Platform Events? Platform Events Developer Guide https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm Platform Events Basics https://trailhead.salesforce.com/en/content/learn/modules/platform_events_basics

Editor's Notes

  1. In 2017 Salesforce introduced platform events to support the Event-Driven software architecture Prior to this integrating distributed systems required tightly coupled point to point integrations such as the request response REST API integrations. Systems in request-response communication models make a request to a web service. In comparison, systems in an event-driven architecture react to an event in near real time when the event occurs. Event producers don’t know the consumers that receive the events. Any number of consumers can receive and react to the same events. The only dependency between producers and consumers is the semantic of the message content. Example: eCommerce system publishes a single event that is consumed by several services to handle (payment processing, shipping, inventory updates, …).
  2. Event similar to sObject -> Event message similar to how a record is an instance of a sObject. Note the API name suffix of __e instead of __c Published events cannot be edited/deleted. To consume the event, an app can subscribe to it (as opposed to querying it). Custom fields become the content or semantics of the event message. Publish Behavior: Publish After Commit. When subscriber dependent on data to be present or if you simply don’t want an event to publish if transaction fails. Publish Immediately. Without fear of rollback. This is especially convenient for logging purposes so that you don’t have to relay on debug logs.
  3. Not following best practices here for brevity In this example I want to publish the event when the case JIRA Status is changed to Awaiting Reply Populating and instantiating the same platform event we saw earlier. EventBus.publish() method pushes the message to the channel. Behavior is similar to Database.insert() where you can interrogate the Database.SaveResult. Apex governor limits apply. Each EvenBus.publish execution counts as a DML statement. You can also capture the Database.SaveResult object to determine whether the operation was successful or not in same fashion as you would insert or update sObjects.
  4. For Flows, add a Create Records element to the appropriate flow. Where you'd usually pick an object to create, select the custom platform event.
  5. Such as SOAP API, REST API, or Bulk API. POST request to the endpoint Request body will contain a JSON formatted message containing values for any of the fields defined for the event.
  6. We’ll look at some examples of subscribing by trigger and process and talk a little about the lightning components BUT we won’t explicitly go over CometD
  7. Not following best practices here for brevity Very similar to trigger for any sObject The after insert trigger event corresponds to the time after a platform event is published Along with processes, it is the most straight forward method of consuming published events Trigger Batch size: The Apex trigger batch size for platform events is 2,000 as opposed to trigger batch size of 200 for standard/custom objects.
  8. Build the process to start when it receives a platform event message. In the process’s trigger, associate the process with a platform event and an object. The object you choose determines with object fields you can access throughout your process. The process looks for a case record whose Id matches the case id in the event message. Flow Builder subscribes to events by using a Pause element instead of starting of flow when a platform event occurs. When the event is receives the flow resumes.
  9. Using empApi component is slightly different for Lightning Web Components versus Aura Components but both involve defining the empApi component and utilizing the empApi components methods of subscribe, unsubscribe, etc.
  10. Visualforce pages can use CometD as well. CometD is a scalable WebSocket and HTTP based event and message routing bus. CometD is a set of library to write web applications that perform messaging over the web. The replay ID is a system generated value, and it is used to position the event in the event stream.
  11. Great for situations where the error is likely to go away later. Could be timeouts, row locks. Or as simple as a field value change. Events are resent after a small delay that increases in subsequent retries. Retrying the event causes all events in the batch to be resent. May retry events 9 times (the initial run plus 9 retries). Make sure to retry LESS THAN 9 times. After the ninth retry, the trigger moves to an error state and must then be fixed and saved.
  12. Unhandled exceptions such as CPU or heap size limits. Intentionally designing trigger batch size limits or some other processing logic. Because trigger batch sizes can be 2000 instead of normal 200, you can reach some limits more readily. New batch starts with event message after the one with replay ID you set.
  13. Lightning Email Templates are not available for use in Email Alerts, so basic Process Builder or Workflow Rules are not an option. Additionally, there are dozens of these labels with the corresponding email templates which complicates the logic needed to implement with Process Builder or Workflow Rule. Since the label values match the email template names, I can query the needed email templates through Apex and send the notification to the customer.