SaasGuru Logo

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!

Flat 20% OFF on Salesforce Marketing Cloud and Data Cloud + AI Bootcamp 🎉– Offer Ends 30th Nov!
Salesforce Trigger Interview Prep Scenarios With Solution

Salesforce Trigger Interview Prep Scenarios With Solution

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

        }

    }

}

saasguru Salesforce Labs: Real-time Projects for Practice

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.

FPe71x0I84wwGX3TH BCB4kQFlx EQllYBc52yLHzimU5cNQfwXJ4OxGHS2UWZ7UGXvKvTglNBVSpvAE0TvKmRTIPmoMCk2jTSAIQGP xqMoEPrki64OwK40atIFsE

Explore and practice 100+ scenarios with saasguru Salesforce Labs. You can also connect your Salesforce Developer account and check your answers. Sign Up Now!

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Humans of Salesforce – Mauricio Alexandre Silva

Discover Mauricio Alexandre Silva’s inspiring Salesforce journey, tips for success, and insights into the evolving ecosystem. Read now!

Salesforce India Achieves $1 Billion Revenue Milestone

Salesforce India achieves $1 billion revenue milestone, driven by AI, innovation, and digital transformation across industries. Read now!

Most Demanded Courses in IIT Kanpur in 2025

Explore IIT Kanpur’s most demanded courses in 2025, including Salesforce, AI, Data Science, and more. Read now!