Lead Rating Auto-Update Trigger for Company Size for FedEx
Imagine you’re working with FedEx’s sales team, which uses Salesforce to manage leads. FedEx wants to automatically update the lead’s rating when the number of employees associated with a lead’s company exceeds 50. If the number of employees is greater than 50, the lead’s rating should be automatically changed to “Hot.” Your goal is to create a trigger that will handle this update automatically for all leads associated with FedEx.
Step 1: Create a Trigger on Standard Lead object
Trigger Name: LeadTrigger
Trigger Handler Class Name: LeadTriggerHandler
Description: This trigger is designed to automatically update the rating of a lead when the number of employees associated with the lead’s company exceeds 50.
Hint
// Trigger Name: LeadTrigger
// Trigger Events: Before Insert, Before Update
trigger LeadTrigger on Lead (before insert, before update) {
// Check if the trigger context is after the record(s) have been inserted or updated
if (Trigger.isBefore) {
if (Trigger.isInsert) {
// Call the appropriate handler method based on the trigger event
}
if (Trigger.isUpdate) {
// Call the appropriate handler method based on the trigger event
}
}
}
Step 2: Create a LeadTriggerHandler class
Trigger Handler Class Name: LeadTriggerHandler
Methods: handlerAfterInsert, handlerAfterUpdate
Description: The LeadTriggerHandler class contains methods to handle the logic for the trigger. The handlerAfterInsert method should be called after a new lead record is inserted, and the handlerAfterUpdate method should be called after a lead record is updated.
Hint
public class LeadTriggerHandler {
// Handler method to handle logic before lead records are inserted
public static void handlerBeforeInsert(List<Lead> newLeads) {
// Iterate through new lead records using a for-each loop
for (Lead lead : newLeads) {
// Check if the value of the NumberOfEmployees field is greater than 50
if (YOUR_CONDITION_HERE) { // Hint: Use ‘>’ for comparison
// Inside the condition, update the Rating field of the lead to ‘Hot’
}
}
}
// Handler method to handle logic after lead records are updated
public static void handlerBeforeUpdate(List<Lead> updatedLeads, Map<Id, Lead> oldLeadMap) {
// Iterate through updated lead records using a for-each loop
for (Lead lead : updatedLeads) {
// Inside the loop, get the corresponding old lead record using oldLeadMap.get(lead.Id)
// Check if the value of the NumberOfEmployees field has changed from old value to new value
if (YOUR_CONDITION_HERE) { // Hint: Compare old and new values
// Inside the condition, update the Rating field of the lead to ‘Hot’
}
}
}
}
Step 3: Validate logic for new lead record insert and lead record update
Verify the logic in the “LeadTriggerHandler” Apex Class when a new lead record is added and the number of employees is greater than 50.
Explore and practice 100+ scenarios with saasguru Salesforce Labs. You can also connect your Salesforce Developer account and check your answers. Sign Up Now!