You made it to the end of week 2! This quick test is made to test your basic understanding of everything you’ve learnt til now!

  • You can look at the syntax, bonus points if you don’t.
  • The test should ideally not take more than 20 minutes to solve.
  • All the criterias must be met.
  • The answer is available at Admin2Dev GitHub Repo.
  • The answer and complete breakdown of the program is available at the end of this page.

The Test

Question

Write a Trigger oppUpdate and a helper class must be named oppUpdateHelper with methods that:

  • Creates a new Task whenever an Opportunity is inserted.
  • Creates a new Task whenever an Opportunity is updated, unless it is ‘Closed Lost’ or ‘Closed Won’. The task subject should match it’s current stage.
  • Creates a new Task if the opportunity changes from ‘Needs Analysis’ to ‘Closed Lost’.

Start a timer and note how long you take to solve. Ideally, it should take ~30 minutes, but don’t worry if you go overboard.


Answer

Creating a task when an opportunity is inserted

Trigger

trigger oppUpdater on Opportunity (after insert) {

    if(Trigger.isafter){
        if (Trigger.isInsert){
            oppUpdaterHelper.oppInsertTask(trigger.new);

        }
    }
}

Class Helper

public class oppUpdaterHelper{

    public static void oppInsertTask(List<Opportunity> oppList){

        List<Task> finalList = new List<Task>();

        for (Opportunity iterator: oppList){
            Task newTask = new Task();
            newTask.WhatId = iterator.id;
            newTask.Subject = 'New task';
            finalList.add(newTask);
        }
        insert finalList;
    }

}

Create a New task unless ‘Closed Won’ or ‘Closed Lost’

Trigger

trigger oppUpdater on Opportunity (after insert) {

    if(Trigger.isafter){
        if (Trigger.isInsert){
            oppUpdaterHelper.oppInsertTask(trigger.new);
            oppUpdaterHelper.oppClosedTask(trigger.new);
        }
    }
}

Class Helper

public class oppUpdaterHelper{

    public static void oppInsertTask(List<Opportunity> oppList){

        List<Task> finalList = new List<Task>();

        for (Opportunity iterator: oppList){
            Task newTask = new Task();
            newTask.WhatId = iterator.id;
            newTask.Subject = 'New task';
            finalList.add(newTask);
        }
        insert finalList;
    }

    public static void oppClosedTask(List<Opportunity> oppList){

        List<Task> finalList = new List<Task>();

        for (Opportunity iterator: oppList){
            if (iterator.StageName == 'Closed Won' || iterator.StageName == 'Closed Lost'){
                //Do nothing. Optionally, use != operator
            }
            else{
                Task newTask = new Task();
                newTask.WhatId = iterator.id;
                newTask.Subject = iterator.StageName;
                finalList.add(newTask);
            }
        }
        insert finalList;
    }

}

Creates a new Task if the opportunity changes from ‘Needs Analysis’ to ‘Closed Lost’.

Trigger

trigger oppUpdater on Opportunity (after insert, after update) {

    if(Trigger.isafter){
        if (Trigger.isInsert){
            oppUpdaterHelper.oppInsertTask(trigger.new);
            oppUpdaterHelper.oppClosedTask(trigger.new);
        }

        if (Trigger.isUpdate){
            oppUpdaterHelper.oppChangeTask(trigger.oldMap, trigger.new);
        }
    }
}

Class Helper

public class oppUpdaterHelper{

    public static void oppInsertTask(List<Opportunity> oppList){

        List<Task> finalList = new List<Task>();

        for (Opportunity iterator: oppList){
            Task newTask = new Task();
            newTask.WhatId = iterator.id;
            newTask.Subject = 'New task';
            finalList.add(newTask);
        }
        insert finalList;
    }

    public static void oppClosedTask(List<Opportunity> oppList){

        List<Task> finalList = new List<Task>();

        for (Opportunity iterator: oppList){
            if (iterator.StageName == 'Closed Won' || iterator.StageName == 'Closed Lost'){
                //Do nothing. Optionally, use != operator
            }
            else{
                Task newTask = new Task();
                newTask.WhatId = iterator.id;
                newTask.Subject = iterator.StageName;
                finalList.add(newTask);
            }
        }
        insert finalList;
    }

    public static void oppChangeTask(Map<ID, Opportunity> oldList, List<Opportunity> newList){


        for(Opportunity iterator: newList){

            Opportunity oldOpp = oldList.get(iterator.id);

            Boolean oldValue = oldOpp.stageName.equals('Needs Analysis');
            Boolean newValue = iterator.StageName.equals('Closed Lost');

            if(oldValue && newValue){
                Task newTask = new Task();
                newTask.WhatId = iterator.id;
                newTask.Subject = 'Closed Lost';
                insert newTask;
            }
        }
    }

}
Day 14: Governor Limits and Batch Apex