Step 4: Go to Setup ➤ Object Manager tab ➤ Lead ➤ Triggers, and click the “New” button.
trigger FieldChangeTracking on Lead (after update) {
String fieldToTrack = ‘Rating’;
Lead oldLead = Trigger.old[i];
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))
);
}
}
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) {
Name = ‘New Opportunity’, StageName = ‘Prospecting’,
CloseDate = Date.today().addDays(30), // Set the close date as needed
AccountId = acc.Id
);
newOpportunities.add(newOpportunity);
}
}
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.