function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vrushali Mali 4Vrushali Mali 4 

Getting error while studying this trailhead https://trailhead.salesforce.com/modules/apex_triggers/units/apex_triggers_bulk

Hi,
 This is my trigger : can any one help me where i am wrong.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    
    //List<Task> tasklst = [select id from Task limit 200]; 
    for(Opportunity opp : Trigger.new){
        
        //for(Opportunity op : opplst){
        if(opp.StageName == 'Closed Won'){
        
          
           Task t = new Task();
           t.WhatId = opp.id;
           t.Subject = 'Follow Up Test Task';
           t.Ownerid = opp.Ownerid;
           t.ActivityDate = system.today();

           insert t;
           system.debug('+++++++t++++++'+t);
        
       
    
    }
    }

}
Best Answer chosen by Vrushali Mali 4
Amit Chaudhary 8Amit Chaudhary 8
HI Vrushali Mali 4,

Issue in your code is that you are doind DML (insert) inside the for loop.  Update your code like below
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) 
{
 	List<Task> lstTaskToInsert = new List<Task>();
    for(Opportunity opp : Trigger.new)
	{
        
        if(opp.StageName == 'Closed Won')
		{
			   Task t = new Task();
			   t.WhatId = opp.id;
			   t.Subject = 'Follow Up Test Task';
			   t.Ownerid = opp.Ownerid;
			   t.ActivityDate = system.today();

			   lstTaskToInsert.add(t);
			   system.debug('+++++++t++++++'+t);    
		}
    }
	
	if( lstTaskToInsert.size()>0 )
	{
		insert lstTaskToInsert;
	}
}



Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000AndqIAC

Let us know if this will help you
 

All Answers

Velvel Marasow 10Velvel Marasow 10
What is the Error that you're getting?
You shouldn't be doing DML operations withing a loop, that may be what's causing it.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Can you please share the error message you are getting?

Also, if you are working on bulkification of triggers then you should not insert task inside a loop. Instead, add the tasks in a list and insert the list outside the for loop.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    
    //List<Task> tasklst = [select id from Task limit 200]; 
List<Task> lstTask = new List<Task>();
    for(Opportunity opp : Trigger.new){
        
        //for(Opportunity op : opplst){
        if(opp.StageName == 'Closed Won'){
        
          
           Task t = new Task();
           t.WhatId = opp.id;
           t.Subject = 'Follow Up Test Task';
           t.Ownerid = opp.Ownerid;
           t.ActivityDate = system.today();

           lstTask.add(t);
           system.debug('+++++++t++++++'+t);    
           }
    }
if(lstTask.size()>0)
insert lstTask;

}

 
Amit Chaudhary 8Amit Chaudhary 8
HI Vrushali Mali 4,

Issue in your code is that you are doind DML (insert) inside the for loop.  Update your code like below
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) 
{
 	List<Task> lstTaskToInsert = new List<Task>();
    for(Opportunity opp : Trigger.new)
	{
        
        if(opp.StageName == 'Closed Won')
		{
			   Task t = new Task();
			   t.WhatId = opp.id;
			   t.Subject = 'Follow Up Test Task';
			   t.Ownerid = opp.Ownerid;
			   t.ActivityDate = system.today();

			   lstTaskToInsert.add(t);
			   system.debug('+++++++t++++++'+t);    
		}
    }
	
	if( lstTaskToInsert.size()>0 )
	{
		insert lstTaskToInsert;
	}
}



Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000AndqIAC

Let us know if this will help you
 
This was selected as the best answer
Vrushali Mali 4Vrushali Mali 4
Thanks Amit Chaudhary 8