Trigger.size 3 – Create Triggers with Context Variables – Triggers and Bulk Processing – Salesforce Certified Platform Developer I Study Guide

Step 4: Go to Setup ➤ Object Manager tab ➤ Lead ➤ Triggers, and click the “New” button.

  • This trigger captures changes in the ‘Rating’ field for Lead records.
  • It leverages Trigger.size for efficient bulk processing.

trigger FieldChangeTracking on Lead (after update) {

  • List to store History__c records for field changes List<History__c> historyRecords = new List<History__c>();
  • Field API name to track changes

String fieldToTrack = ‘Rating’;

  • Iterate through the updated records using Trigger.size for (Integer i = 0; i < Trigger.size; i++) {
  • Retrieve new and old versions of the Lead record Lead newLead = Trigger.new[i];

Lead oldLead = Trigger.old[i];

  • Compare old and new values of the specified field (‘Rating’) if (oldLead != null && newLead.get(fieldToTrack) != oldLead. get(fieldToTrack)) {
  • Create a new History__c record to capture the field change History__c history = new History__c(

Object_Id__c = newLead.Id, Field_Name__c = fieldToTrack,

Old_Value__c = String.valueOf(oldLead.get(fieldToTrack)), New_Value__c = String.valueOf(newLead.get(fieldToTrack))

);

  • Add the history record to the list historyRecords.add(history);

}

}

  • Insert history records into the database if changes were detected if (!historyRecords.isEmpty()) {

insert historyRecords;

}

}

In this example:

•\ The trigger is set to execute after update on the Lead object.

•\ The trigger iterates through the records in Trigger.newMap.

•\ For each record, it retrieves the corresponding old version from

Trigger.oldMap using the record’s ID.

•\ It compares the old and new values of the specified field Rating.

•\ If the field value has changed, it creates a new History__c record to

capture the change.

•\ The history records, accumulated using Trigger.size, are then inserted

into the database.

By using Trigger.oldMap in this example, you can access the previous values of the records before the update and compare them with the new values. This allows you to implement advanced logic that reacts to specific changes in your data and performs related actions.

Now, you can change the values in the Rating field after creating new leads on your Lead object and track the value changes in the newly created customer object’s History__c.

Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, Trigger.isUndelete

These are context variables that provide information about the type of operation that caused the trigger to fire. These variables are contained in the System.Trigger class and are commonly used to control the flow of logic within triggers.

•\ Trigger.isInsert: Returns true if the trigger was fired due to an insert operation. This includes inserts performed through the Salesforce user interface, Apex code, or the API.

•\ Trigger.isUpdate: Returns true if the trigger was fired due to an update operation. This includes updates performed through the Salesforce user interface, Apex code, or the API.

•\ Trigger.isDelete: Returns true if the trigger was fired due to a delete operation. This includes deletions performed through the Salesforce user interface, Apex code, or the API.

•\ Trigger.isUndelete: Returns true if the trigger was fired after a record is recovered from the Recycle Bin. This includes undelete operations performed through the Salesforce user interface, Apex code, or the API.

In the following example, we want to automate the creation of an opportunity record whenever a new account is inserted into Salesforce. You can use the Trigger.isInsert context variable to determine if the trigger event is an insert and then create a related opportunity record.

Go to Setup ➤ Object Manager tab ➤ Account ➤ Triggers, and click the “New” button.

trigger CreateOpportunityOnAccountInsert on Account (after insert) { List<Opportunity> newOpportunities = new List<Opportunity>();

for (Account acc : Trigger.new) {

if (Trigger.isInsert) {

  • Create an opportunity with account information Opportunity newOpportunity = new Opportunity(

Name = ‘New Opportunity’, StageName = ‘Prospecting’,

CloseDate = Date.today().addDays(30), // Set the close date as needed

AccountId = acc.Id

);

newOpportunities.add(newOpportunity);

}

}

  • Insert the new opportunities

if (!newOpportunities.isEmpty()) { insert newOpportunities;

}

}

In this example:

•\ The trigger is set to execute after insert on the Account object.

•\ The trigger iterates through the new account records in Trigger.new.

•\ The trigger uses Trigger.isInsert to confirm that the event is an insert

operation.

•\ For each new account, a corresponding opportunity is created with

a default name, StageName, CloseDate, and the account’s ID is associated.

•\ The new opportunity is inserted into the database.

By using this Trigger.isInsert example, you ensure that the opportunity creation logic only runs when new accounts are inserted.