What is Asynchrounus Apex Trigger?
How Asynchronous Apex Trigger is different from Apex Trigger?
What is Change Data Capture?
What is the difference between Aynchronous Apex Trigger, Future method and Queueable jobs?
What is the use of Change Data Capture and Asynchronous Apex Trigger?
What is the difference between Salesforce Change Data Capture vs Platform Events?
Demystifying all your doubts related to change data capture and Asynchronous trigger
Change Data Capture (CDC) is a streaming product which enable you to capture changes of salesforce records in real time. In other words, CDC is a set of software design pattern used to determine the data that has changed so that action can be taken using changed data. It publishes events such as create, update, delete, and undelete operations.
Asynchronous Apex triggers are change event triggers that run asynchronously after a database transaction is completed. They are ‘after-insert’ triggers and can be created the same way you create regular Apex triggers, the only difference is the sObject, where you will have change it to the event object. The name of changeEvent object has suffix “changeEvent” for standard object and __changeEvent for custom objects (e.g. OpportunityChangeEvent , Employee_ChangeEvent etc)
What are the objects supported in Change Data Captured?
Standard objects such as Account, Contact, Lead, User, Order, OrderItem, Product2, Task, Opportunity .. etc.
All custom objects
Available in: both Salesforce Classic and Lightning Experience
Available in: Enterprise, Performance, Unlimited, and Developer editions
Use of Asynchronous trigger and Change Data Capture.
1. Asynchronous triggers are the probable replacement for future methods and queuable jobs with certain limitations.
2. Let’s take a simple business example: when a customer logs a case with customer service, case records gets created. If you want write a logic which can execute asynchronously and assign a technician to it then you can write an asynchronous trigger on CaseEvent.
3. Change Data Capture can be used for Integration purpose such as to downstream system (ERP System) and it is much more effective and faster than the normal webservices.
4. CDC stores events up to 3 days so very effective if your downstream system is down for weekly maintenance, it will automatically re trigger once it is up.
Points to be noted on Change Data Capture and Asynchronous Apex trigger
1. Change Data Capture respects your org’s field-level security settings. Delivered events contain only the fields that are subscribed and user is allowed to view.
2. To get debug log of Asynchronous Apex Trigger you need to setup the Traced Entity Type to Automated Process.
3. You can’t call a future method from Asynchronous trigger.
4. It is only written in after insert.
5. They run under the Automated Process entity.
6. They are subject to Apex synchronous governor limits.
7. They have a maximum batch size of 2,000 event.
8. It contains all fields for a particular record.
9. Test.enableChangeDataCapture() must be added in the beginning of the test class. This will only enable for the test class transaction.
How to Subscribe to an Event Channel
Salesforce Home Page -> Setup -> Change Data Capture -> Select Object in Available Entities -> click on ‘>’
Change data received at event bus in below format : Notice entityName and ChangeType fields value below.
How to write Asynchronous trigger?
Salesforce Home -> Developer Console -> File -> New -> Apex Trigger
Name : OpportunityChangeTrigger
Object : OpportunityChangeEvent
Sample Code: To create Task on update of Opportunity stage to Closed Won
/*****************************Code Start here***************************/
trigger OpportunityChangeTrigger on OpportunityChangeEvent (after insert) {
List<Task> tasks = new List<Task>();
for (OpportunityChangeEvent event : Trigger.New) {
// Get event header fields
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
System.debug('Received change event for :' + header.entityName + ' : for the ::' + header.changeType + ': operation.:' + event.StageName);
if (header.changetype == 'UPDATE') { // Header fields
if (event.StageName=='Closed Won') { // Evenet message fields
System.debug('should create task now');
Task tk = new Task();
tk.Subject = 'Follow up on won opportunities: ' + header.recordIds;
tk.OwnerId = header.CommitUser;
tasks.add(tk);
}
}
}
// Insert all tasks in bulk.
if (tasks.size() > 0) {
insert tasks;
}
}
/*****************************Code end here***************************/
Difference Between Asynchronous Apex trigger and Future Method / Queuable jobs
References :
コメント