Scenario Based Apex Trigger Question from Deloitte

During Interview with Deloitte, One of my friend was given a real-world Apex trigger scenario to solve. The task was to handle both insert and update events on the Opportunity object in Salesforce and automatically create a Task when certain conditions were met.

πŸ” Scenario

Write an Apex trigger on the Opportunity object that performs two different actions based on whether the record is inserted or updated. When a new Opportunity is inserted with an Amount greater than 10,000, the trigger should automatically create a Task with the subject “Follow up on new high-value opportunity,” priority set to High, status as Not Started, and the due date set to tomorrow. Additionally, when an existing Opportunity is updated and the Amount field is changed to a value greater than 10,000, another Task should be created with the subject “Re-check updated high-value opportunity,” along with the same values for priority, status, and due date.

βœ… Solution

trigger OpportunityTrigger on Opportunity (after insert, after update) {
    List<Task> tasksToCreate = new List<Task>();

    // Handle INSERT logic
    if (Trigger.isInsert) {
        for (Opportunity opp : Trigger.new) {
            if (opp.Amount != null && opp.Amount > 10000) {
                Task t = new Task(
                    WhatId = opp.Id,
                    Subject = 'Follow up on new high-value opportunity',
                    Priority = 'High',
                    Status = 'Not Started',
                    ActivityDate = Date.today().addDays(1)
                );
                tasksToCreate.add(t);
            }
        }
    }

    // Handle UPDATE logic
    if (Trigger.isUpdate) {
        for (Opportunity opp : Trigger.new) {
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            // Check if Amount was changed and is now greater than 10000
            if (opp.Amount != null && opp.Amount > 10000 &&
                (oldOpp.Amount == null || opp.Amount != oldOpp.Amount)) {
                
                Task t = new Task(
                    WhatId = opp.Id,
                    Subject = 'Re-check updated high-value opportunity',
                    Priority = 'High',
                    Status = 'Not Started',
                    ActivityDate = Date.today().addDays(1)
                );
                tasksToCreate.add(t);
            }
        }
    }

    if (!tasksToCreate.isEmpty()) {
        insert tasksToCreate;
    }
}

πŸ“Œ Takeaway:

This scenario tests not just your Apex syntax, but also how well you understand trigger contexts, real-world automation use cases, and clean logic separation. It’s a great example of how interviews at top firms like Deloitte focus on practical Salesforce development skills.

Author

  • Satyam parasa

    Satyam Parasa is a Salesforce and Mobile application developer. Passionate about learning new technologies, he is the founder of Flutterant.com, where he shares his knowledge and insights.

    View all posts

Leave a Comment