You need to sign in to do that
Don't have an account?

bulkify trigger
i am getting dml statements for this trigger pleaseh help resolve the issue
trigger Auto_Populate_salesteam_create_splits on Opportunity (after insert) { public List<splits__c> spl1{get;set;} spl1 = new List<splits__c>(); for(opportunity o:trigger.new) { //GET ALL ACCOUNT TEAM MEMBERS EXCEPT THE OWNER OF THE OPPORUNITY integer comm =0; opportunityteammember[] otm = [select id,UserId from opportunityteammember where opportunityid=:o.id]; Integer otmcount = otm.size(); system.debug('otmcount:'+otmcount); splits__c spl4oppowner = new splits__c(); spl4oppowner.opportunity__c = o.id; spl4oppowner.user__c = o.ownerid; spl4oppowner.Split__c = (100/(otmcount+1)); insert spl4oppowner; for(opportunityteammember otm1:otm){ comm = (otmcount); splits__c spl = new splits__c(); spl.opportunity__c = o.id; spl.user__c = otm1.userid; spl.Split__c = (100/(comm+1)); //insert spl; //comm= comm+1; spl1.add(spl); } insert spl1; } }
Hi sirikanth,
You need to write your trigger some thing like this.
trigger Auto_Populate_salesteam_create_splits on Opportunity (after insert)
{
public List<splits__c> spl = new List<splits__c>();
public List<splits__c> spl1 = new List<splits__c>();
list <opportunityteammember> oppMember = [select id,UserId from opportunityteammember];
for(opportunity o:trigger.new)
{
//GET ALL ACCOUNT TEAM MEMBERS EXCEPT THE OWNER OF THE OPPORUNITY
integer comm =0;
Integer otmcount = otm.size();
splits__c spl4oppowner = new splits__c();
spl4oppowner.opportunity__c = o.id;
spl4oppowner.user__c = o.ownerid;
spl4oppowner.Split__c = (100/(otmcount+1));
spl.add(spl4oppowner);
if(oppMember .size() >0){
for(opportunityteammember otm1:oppMember ){
comm = (otmcount);
splits__c spl = new splits__c();
if(o.id ==otm1.opportunityid){
spl.opportunity__c = o.id;
spl.user__c = otm1.userid;
spl.Split__c = (100/(comm+1));
spl1.add(spl);
}
}
}
}
if(spl.size() >0){
insert spl;
}
if(spl1.size() >0){
insert spl1;
}
}
Thanks
Shailu
Refer : http://wiki.developerforce.com/page/Apex_Code_Best_Practices
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
Quick observations:
1. Pull your SOQL Query out of the loop - put it outside of the loop and create a map or list of all records - parse them inside the loop.
2. Create an empty (new) List object - add your new record objects to that list and then do a single "insert List" DML statement outside of your loop.
-Andy
Below is your bulkified trigger:
Please let me know if you are facing any issues.
Regards,
Lakshman