You need to sign in to do that
Don't have an account?
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);
}
}
}
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);
}
}
}
Issue in your code is that you are doind DML (insert) inside the for loop. Update your code like below
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
You shouldn't be doing DML operations withing a loop, that may be what's causing it.
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.
Issue in your code is that you are doind DML (insert) inside the for loop. Update your code like below
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=906F0000000AndqIAC
Let us know if this will help you