Subscribe to Change Events Using an Apex Trigger | Change Data Capture Basics

Published: 12 November 2022
on channel: Saurabh Infotech Solutions
2,045
10

#salesforce
#trailhead
#salestraining

Change Data Capture Basics

Asynchronous Apex Triggers for Change Events
Write a Change Event Trigger
Create a change event trigger that captures changes on opportunities and creates a follow-up task for opportunities whose stage is set to 'Closed Won'.
Create an Apex trigger modeled after the EmployeeChangeTrigger example trigger:
Name: OpportunityChangeTrigger
Object: OpportunityChangeEvent
Copy the body of the EmployeeChangeTrigger example trigger. Delete everything after the if statement for the header, starting with the System.debug statements.
Modify the for loop so it iterates over every received OpportunityChangeEvent. For each event, if the header changeType field is 'UPDATE', check if event.isWon field is equal to true. You will end up with the following if statement:
if ((header.changetype=='UPDATE') && (event.isWon==true)) { // Create a task }
Inside the new if statement block, add these lines to create a task.
Task tk = new Task();
tk.Subject = 'Follow up on won opportunities: ' + header.recordIds;
tk.OwnerId = header.CommitUser;
tasks.add(tk);
Insert the task list after the end of the for loop.
if (tasks.size() 0) {
insert tasks;
}


Watch video Subscribe to Change Events Using an Apex Trigger | Change Data Capture Basics online without registration, duration hours minute second in high quality. This video was added by user Saurabh Infotech Solutions 12 November 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,045 once and liked it 10 people.