3

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 'ClosedOpportuni

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

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

    for(Opportunity o: Trigger.new)
    {
        if(o.StageName=='Closed Won' && ( trigger.isInsert || (trigger.isUpdate && trigger.oldMap.get(o.Id).StageName != 'Closed Won') ))
        {
            Task t= new Task();
            t.Subject='Follow up Task';
            t.WhatId=o.Id;
            taskListToInsert.add(t);
        }

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

}

I am getting an 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

9
  • 1
    do you have any other trigger on opportunity ?
    – Himanshu
    Mar 2, 2016 at 17:30
  • No I dont have any other trigger @Himanshu
    – Joy
    Mar 2, 2016 at 17:32
  • 1
    any lightning process ?
    – Himanshu
    Mar 2, 2016 at 17:36
  • You should not write DML statements inside a forloop, which would cause 200 insertions for 200 opportunity insert/update where 150 is the list.So you should use Collections (Lists, Sets, Maps) to store them and insert at once, and it is a best practice to use 'after insert/update' if your transacations are on different objects @Joy
    – Pavan tej
    Mar 2, 2016 at 17:37
  • 1
    Some uses of process builder are also limited by governor limits fyi. Typically with this error the issue is not necessary where you get the exception, but it may be anywhere before that in the execution context. Mar 2, 2016 at 22:21

2 Answers 2

1

I will suggest you to deactivate other working trigger on your opportunity. You can use my code.

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

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

    for(opportunity opp:[SELECT Id,Name,CloseDate, StageName 
                           FROM Opportunity WHERE StageName='Closed Won'])
    {   
            Task t=new Task(); 
            t.Subject = 'Follow Up Test Task';
            t.WhatId = opp.Id;
            tasklist.add(t);
    }
            insert taskList;       
}
1
  • I got the badge.
    – user41013
    Apr 7, 2016 at 6:25
0

it is a sample code like use map instead of list for task

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

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

    for(Opportunity o: Trigger.new)
    {
        if(o.StageName=='Closed Won')
        {
            Task t= new Task();
            //your customized code
        }

    }

    insert taskListToInsert ;
}

@Joy i think it solves your problem

4
  • I have done d same thing...dosnt help..!!
    – Joy
    Mar 2, 2016 at 17:42
  • i had tried to do in my developer org it works perfectly........@Joy can you debug the code
    – Pavan tej
    Mar 2, 2016 at 18:00
  • i think your issue because of other apex classes in your org.Can you any other triggers are there in opportunity and use debug logs and check any other apex classes or triggers executing at that time.And use best practice like don't use dml statements inside loops @Joy
    – Pavan tej
    Mar 2, 2016 at 18:11
  • @Joy I will go with what Pavan says. Probably must be another trigger or class executing on the same event in recursive manner which is causing this error.
    – manjit5190
    Mar 3, 2016 at 13:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .