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

Imagine you want to enforce a validation rule during the conversion of leads in Salesforce. Specifically, you want to ensure that converted leads have a certain minimum lead rating before they can be converted. You can use the Trigger.new context variable to access the leads being converted and perform the validation.

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

trigger LeadConversionValidation on Lead (before update) { List<Lead> leadsToConvert = new List<Lead>();

  • Iterate through the leads being updated (converted) for (Lead lead : Trigger.new) {

if (lead.IsConverted) { leadsToConvert.add(lead);

}

}

  • Check lead scores before conversion

for (Lead convertedLead : leadsToConvert) { if (convertedLead.Rating != ‘Hot’) {

convertedLead.addError

(‘Leads must have a lead rating of “Hot” to be converted.’);

}

}

}

In this example:

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

•\ The trigger iterates through the leads in the Trigger.new collection.

•\ If a lead is being converted (IsConverted is true), it’s added to the

leadsToConvert list.

•\ Another loop checks the lead rating of the leads to be converted.

•\ If a lead’s rating is not “Hot,” an error message is added to the lead using

the addError method. This prevents the conversion from proceeding.

This use case example demonstrates how you can use the Trigger.new context variable to access and manipulate the records being processed in an Apex trigger. By leveraging this variable, you can enforce custom business rules and data validation during various database operations.

Now, if you try to set any value other than “Hot” in the Rating field when converting Leads, you will get an error “Validation error on Lead: Leads must have a lead rating of ‘Hot’ to be converted.”

Please note that Trigger.new and Trigger.old cannot be used in Apex DML operations. These context variables are used to access the records that caused the trigger to fire, but they cannot be used to perform DML operations such as insert, update, or delete on the records. Therefore, if you need to perform DML operations on the records, you should create a separate list or map variable and perform the DML operations on that variable instead of using Trigger.new or Trigger.old.

Trigger.newMap and Trigger.oldMap

They are context variables that provide access to the new and old versions of records in a trigger, represented as maps that associate record IDs with their corresponding records. They are useful for efficiently accessing records and comparing old and new values.

•\ Trigger.newMap: Returns a map of record IDs to the new versions of the sObject records. This map is available in before update, after insert, after update, and after undelete triggers.

•\ Trigger.oldMap: Returns a map of record IDs to the old versions of the sObject records. This map is available in before update, after update, before delete, and after delete triggers.