SaasGuru Logo

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉
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

Salesforce Announces Free AI Certifications & Training for All

As Artificial Intelligence (AI) becomes increasingly vital across industries, acquiring AI skills is essential for professionals looking to stay competitive. To support this growing need, Salesforce is launching free access to their AI Certifications, starting on September 23, 2024. This initiative aims to close the AI skills gap and make AI education more accessible to …

Salesforce Announces Free AI Certifications & Training for All Read More »

Atlas: Transforming Automation With Salesforce’s Agentforce

At Dreamforce 2024, Salesforce introduced Atlas, the advanced AI reasoning engine at the heart of Agentforce. Atlas is designed to automate complex decision-making processes, streamline workflows, and personalize customer interactions. This innovation goes beyond typical AI applications, bringing a level of autonomy and learning that empowers businesses to optimize their operations in real time. What …

Atlas: Transforming Automation With Salesforce’s Agentforce Read More »

Top Dreamforce 2024 Keynote Announcements You Can’t Miss!

Welcome to our Dreamforce 2024 live blog, where we’ll cover the biggest announcements from this year’s event. Here’s what you can expect: Key insights on Salesforce’s Agentforce for autonomous agents. Major AI advancements and a $1B investment in AI technologies. Exciting partnerships with IBM and Google to extend agent capabilities. Data Cloud enhancements for faster, …

Top Dreamforce 2024 Keynote Announcements You Can’t Miss! Read More »