Wednesday, January 27, 2016

Trailhead Bulk Apex Triggers

Create an Apex trigger for Opportunity that adds a task to any opportunity set to 'Closed Won'

To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.
  • The Apex trigger must be called 'ClosedOpportunityTrigger'
  • With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
  • To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
  • This challenge specifically tests 200 records in one operation.


1)Write a trigger on opportunity object and make sure it is active while taking challenge.


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

    List<Task> taskList = new List<Task>();
    
    for(Opportunity opp : Trigger.new) {
        
        //Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
        if(Trigger.isInsert) {
            if(Opp.StageName == 'Closed Won') {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }
        
        //Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
        if(Trigger.isUpdate) {
            if(Opp.StageName == 'Closed Won' 
            && Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
                taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
            }
        }       
    }

    if(taskList.size()>0) {        
        insert taskList;        
    }    
}

6 comments:

  1. my code is same but error shows that
    Executing against the trigger does not work as expected.

    ReplyDelete
  2. Challenge not yet complete in neerajdhare@leavemanagement.com
    There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Opportunity Management” process failed. Give your Salesforce admin these details. This error occurred when the flow tried to create records: REQUIRED_FIELD_MISSING: Required fields are missing: [AccountId]. You can look up ExceptionCode values in the SOAP API Developer Guide. Error ID: 903373101-34848 (-667225970): []

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This Works for Sure and easy to understand:
    trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    List taskList = new List();
    for(Opportunity o: Trigger.New){
    if(o.StageName == 'closed won'){
    taskList.add(new Task(Subject='Follow Up Test Task', whatId=o.Id));
    }
    }

    if(taskList.size() > 0){
    insert tasklist;
    }
    }

    ReplyDelete