Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!

Earn a Salesforce AI Certification by IIT + 12-Week Internship Secure Your Spot Today!
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

Mass Assign User Permission Set in Salesforce

Learn how to efficiently mass assign user permission sets in Salesforce using tools like Data Loader. Save time and ensure accuracy. Read now!

How to Clone a FlexCard in Salesforce

Learn how to clone a FlexCard in Salesforce with this step-by-step guide. Simplify customization and boost efficiency effortlessly!

Salesforce Introduces Agentforce 2.0: Redefining How Work Gets Done

Salesforce introduces Agentforce 2.0, the AI platform transforming digital labor with autonomous agents and seamless workflows. Read now!