For the first time ever, the Salesforce Platform has the capability to execute truly asynchronous Apex Triggers. To deliver this feature, we have enabled Apex Triggers for Change Data Capture events. During this session we will enable Change Data Capture in our org, refactor a piece of existing code to execute asynchronously, and discuss other use-cases where this pattern could be used. Come learn how to speed up transactions for end users and decrease your frustration with limits by moving trigger logic into asynchronous triggers.
The Codex of Business Writing Software for Real-World Solutions 2.pptx
TrailheaDX 2019 : Truly Asynchronous Apex Triggers using Change Data Capture
1. Truly Asynchronous Apex Triggers
Using Change Data Capture
jbrock@salesforce.com, @_ johnbrock
John Brock
Product Management
Yasaman Mohsenin
Engineering Management
ymohsenin@salesforce.com
2. This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the
assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we
make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber
growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any
statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services.
The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new
products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays
in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible mergers and acquisitions, the
immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth,
new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger
enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form
10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important
disclosures are available on the SEC Filings section of the Investor Information section of our Web site.
Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be
delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available.
Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
Statement under the Private Securities Litigation Reform Act of 1995
Forward-Looking Statement
3. Key Systems Need Up-To-Date Information
Businesses need to replicate changes in Salesforce to maintain consistency
Account {
Name : ”Acme"
}
Account {
?
}
Account {
?
}
New Account=’Acme’;
CRM Other Key Applications
4. Capture changes without custom code
Easily select objects to capture data changes on with clicks
Keep your key systems up to date
Ensure downstream systems always have the right data
Scale with ease to millions of events daily
Events are held for 72-hours to prevent data loss
Change Data Capture
Generally
Available
12/18
Detect changes on Salesforce records in real-time
No polling
No batch ETL processes
No point-to-point integrations
“/data/ChangeEvents” Channel
Account {
Name : “Acme”
}
Account {
Name :
“Acme”
}
Account {
Name :
“Acme”
}
5. Change Data Capture Unlocks Real-Time Data Sync
Data Replication
Process
Automation
Audit &
Compliance
Salesforce Event Bus
/closed_won
/new_account
/new_account
/data_change
Update external
systems to reflect
changes in Salesforce
Downstream systems
react when Salesforce
data changes
Changes in Salesforce
must be stored for an
extended period of time
7. Apex Triggers
Customizable business logic written natively on Platform
Transactional Execution
Trigger logic is executed and is guaranteed to be complete
before the data is committed
Seamless Interaction Experience
End-users get instant feedback for input errors, logic
decisions, and computation results
Tightly Coupled with DML Operations
All processing, triggers, updates are considered one unit of
work that completely succeeds or rolls back
trigger CaseTrigger on Case (after insert) {
// Setup
....
// Perform logic
....
// Cleanup / Finish
....
}
8. Async Apex Triggers
Complex computations and logic executed outside of the transaction
True Post-Commit Execution
Triggers execute asynchronously after a transaction
Faster End-user Experience
Less logic and computation inside the transaction leads to
quicker end-user experiences and fewer limit concerns
Event-driven and Decoupled
Errors in processing won’t rollback or impact the transaction
trigger CaseChangeEventTrigger on CaseChangeEvent (after insert) {
// Setup
....
// Perform heavy (slow) computation
....
// Cleanup / Finish
....
}
9. When should I use Async Triggers?
Any calculation or piece of logic that can happen after the data is changed
Keep critical logic in the transaction
Data validation, data consistency, and required prerequisite
operations before data is changed
Processing that can happen “later”
-- Heavyweight computations
-- Einstein (or other ML/AI) operations
-- Notifications to other systems
-- Joining of data from different sources
-- Summary calculations
trigger CaseChangeEventTrigger on CaseChangeEvent (after insert) {
List<CaseChangeEvent> changes = Trigger.new;
Set<String> caseIds = new Set<String>();
// Get all Record Ids for this change
for (CaseChangeEvent change : changes) {
List<String> recordIds = change.ChangeEventHeader.getRecordIds();
caseIds.addAll(recordIds);
}
// Perform heavy (slow) computation determining Red Account
RedAccountPredictor predictor = new RedAccountPredictor();
Map<String, boolean> accountsToRedAccountStatus =
predictor.predictForCases(new List<String>(caseIds));
// Publish platform events for predicted red accounts
List<Red_Account__e> redAccountEvents = new List<Red_Account__e>();
for (String acctId : accountsToRedAccountStatus.keySet()) {
if (rating=='Red') {
redAccountEvents.add(new Red_Account__e(Account_Id__c=acctId, Rating__c=rating));
}
}
if (redAccountEvents.size() > 0) {
EventBus.publish(redAccountEvents);
}
}
12. Dive into Change Data Capture on Trailhead!
Learn more
Details, use-cases, and getting-started guides
[Blog] What is Change Data Capture
[Blog] What Streaming Event Should I Use?
Dreamforce 18 : Change Data Capture (20 min)
Dreamforce 18 : Platform Events (20 min)
Dev Guide : Change Data Capture
Get Hands-On with
Automation Workshops
Build Together
on the Low
Code Platform
Thurs. 11am PT
See demos, join a workshop,
ask questions & more!
Visit us at Platform Point
Floor 1
3rd Floor
Keynote Room
Super Sessions:
Salesforce <3 DX
Weds at 3:00 PM
Build Together
with Low Code
Thurs at 11:00 AM