0

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.

Ans:

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    List<Task> myTasklist       =    new List<Task>();
        for(Opportunity myOpp : trigger.new){
        if(myOpp.StageName == 'Closed Won'){
            Task myTask = new Task();
            myTask.Subject      =    'Follow Up Test Task';
            myTask.WhatId       =    myOpp.ID;
            myTasklist.add(myTask);           
        }   
    }
    insert myTasklist;
}

Error:

Challenge Not yet complete... here's what's wrong: There was an unexpected error in your org which is preventing this assessment check from completing: System.LimitException: Too many DML statements: 151

  • 3
    Code looks ok. Do you have a non-bulkified trigger on the task object? – Eric Sep 7 '16 at 22:31
  • 1
    Confused by your comment, you want to know how to write a non-bulkified trigger? – Eric Sep 7 '16 at 22:38
  • 1
    Did you check that you don't have any other Opportunity triggers running DML statements in your org? Only with that trigger inside an org, it's impossible to get that error. – Joca Sep 7 '16 at 22:48
  • 2
    @SfdcBat - Or I am not listening properly :) - A non-bulkified trigger is one that has DML (Insert/update/delete) inside of a loop (typically a For loop). Look at your other trigger on Opportunity/Task for this as the OP code is bulkified – Eric Sep 7 '16 at 23:04
  • 1
    Thank you guys... I just found in other trigger I wrote DML statement inside a FOR loop... – manideep Sep 8 '16 at 1:47
1

The obvious answer is that you have other triggers on Opportunity, and that one of them is not properly bulkified. This is one of the many reasons One Trigger Per Object is listed first in Trigger Frameworks and Apex Trigger Best Practices. I highly recommend adopting a handler pattern and beginning to work on One Trigger Per Ojbect, even as you work through Trailhead modules.

  • 1
    @manideep - or possibly, triggers on Task that do non-bulkified DML – cropredy Sep 8 '16 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.