+ Start a Discussion
Deepak Sharma 184Deepak Sharma 184 

Hello, i am getting an error while doing trailhead, actually my trigger is working, but when i do check challenge it shows error, below is snapshot attached.

trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) 
{
    list<Task> opplist= new list<Task>();
    
  for(opportunity opp:[select id, StageName from opportunity where StageName='Closed Won' AND ID IN: Trigger.New])
  {
      if(Trigger.isInsert || Trigger.isUpdate && opp.StageName =='Closed Won')
      {
          opplist.add(new Task(Subject='Follow Up Test Task' ,WhatId=opp.Id));
      }
          
      if(opplist.Size()>0)
      insert opplist;
  }
}User-added image
Best Answer chosen by Deepak Sharma 184
Amit Chaudhary 8Amit Chaudhary 8
Issue in your code is that you are adding DML (insert ) inside the For loop
  for(opportunity opp:[select id, StageName from opportunity where StageName='Closed Won' AND ID IN: Trigger.New])
  {
      if(Trigger.isInsert || Trigger.isUpdate && opp.StageName =='Closed Won')
      {
          opplist.add(new Task(Subject='Follow Up Test Task' ,WhatId=opp.Id));
      }         
      if(opplist.Size()>0)
      insert opplist;

  }

That should be like below
  for(opportunity opp:[select id, StageName from opportunity where StageName='Closed Won' AND ID IN: Trigger.New])
  {
      if(Trigger.isInsert || Trigger.isUpdate && opp.StageName =='Closed Won')
      {
          opplist.add(new Task(Subject='Follow Up Test Task' ,WhatId=opp.Id));
      }
          
  }
      if(opplist.Size()>0)
      insert opplist;
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000AndqIAC
2)
 
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;        
    }    
}


 
Deepak Sharma 184Deepak Sharma 184
Hi Amit, Thank you for the answer. i just want to know where is my code wrong here. trigger is working. for an opportunity it is created a task. But at the time of check challenge, it is showing error. could u tell me where iam wrong here. it would be great help.

Regards,

Deepak
Amit Chaudhary 8Amit Chaudhary 8
Issue in your code is that you are adding DML (insert ) inside the For loop
  for(opportunity opp:[select id, StageName from opportunity where StageName='Closed Won' AND ID IN: Trigger.New])
  {
      if(Trigger.isInsert || Trigger.isUpdate && opp.StageName =='Closed Won')
      {
          opplist.add(new Task(Subject='Follow Up Test Task' ,WhatId=opp.Id));
      }         
      if(opplist.Size()>0)
      insert opplist;

  }

That should be like below
  for(opportunity opp:[select id, StageName from opportunity where StageName='Closed Won' AND ID IN: Trigger.New])
  {
      if(Trigger.isInsert || Trigger.isUpdate && opp.StageName =='Closed Won')
      {
          opplist.add(new Task(Subject='Follow Up Test Task' ,WhatId=opp.Id));
      }
          
  }
      if(opplist.Size()>0)
      insert opplist;
 
This was selected as the best answer
Deepak Sharma 184Deepak Sharma 184
Thanks Amit, Now its working