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
srikanth11srikanth11 

bulkify the triggers

these are the two trigger splz help me bulkify them

 

trigger Auto_Populate_salesteam_create_splits on Opportunity (after insert) 
{
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;

}
}

}
trigger 2:
trigger Auto_Populate_salesteam on Opportunity (after insert) 
{
public list<opportunityteammember> o1 {get;set;}
o1 =new list<opportunityteammember>();
for(opportunity o:trigger.new)
{
//GET ALL ACCOUNT TEAM MEMBERS EXCEPT THE OWNER OF THE OPPORUNITY

accountteammember[] atm=[select id,UserId,TeamMemberRole from accountteammember 
where userid!=:o.ownerid AND(accountid=:o.Primary_Agency_Buyer__c or accountid=:o.brand__c or accountid=:o.Accountid)];
for(accountteammember atm1:atm)
{
//if(o.ownerid!=atm1.userid){
//FOR EACH ACCOUNT TEAM MEMBER CREATE A NEW OPPORTUNITY TEAM MEMBER ON OPPORTUNITY
opportunityteammember otm = new opportunityteammember();
otm.userid=atm1.userid;
otm.opportunityid=o.id;
otm.TeamMemberRole = atm1.TeamMemberRole;
o1.add(otm);
//}
}
insert o1;
}
}

 

 

salesforce expertsalesforce expert

For Trigger 1:

 

you need to remove the select query inside the for loop. try using a collection list, map or set to get all opportunity ids. this will avoid too many queries exception and way to bulkify your trigger.

 

For Trigger 2:

 

follow the same steps as for trigger 1 given above. add all the records to insert or update in a collecion and finally commit them to db. this way you could optimize the code by cutting down too many dml transactions

 

let me know if it helps! Otherwise i would try to work on your code!

 

Baskaran

srikanth11srikanth11

Hi i still am unable to use maps to bulkify my trigger if possible plz can u give me the code so that i can use it from next time onwards

ajmerakavitaajmerakavita

Hi

 

This will guide you ...

Basically read about collections list,set,map. ...

 

 

trigger Auto_Populate_salesteam_create_splits on Opportunity (after insert)
{

Set<Id> opportunityIdSet = new Set<Id>();
for(opportunity o:trigger.new)
{
 opportunityIdSet.add(o.Id);
}
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;

List<Splits__c> s1 = new List<Splits__c>();

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));
s1.add(sp1);
}
if(s1.size() >0)
insert s1;
}