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 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;
}

}

 

ShaTShaT

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

 

 

Chamil MadusankaChamil Madusanka

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.

Andy BoettcherAndy Boettcher

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

LakshmanLakshman

Below is your bulkified trigger:

 

trigger Auto_Populate_salesteam_create_splits on Opportunity (after insert) 
{
	public List<splits__c> spl1{get;set;}  
	   spl1 = new List<splits__c>();

	Map<ID, List<OpportunityTeamMember>> otmMap = new Map<ID, List<OpportunityTeamMember>>();
	for(OpportunityTeamMember otm : [SELECT id,UserId FROM OpportunityTeamMember where OpportunityID in :Trigger.NewMap.keySet()]) {
		 List<OpportunityTeamMember> otmList = new List<OpportunityTeamMember>();
		 if(otmMap.containsKey(otm.OpportunityId)) {
			 otmList = otmMap.get(otm.OpportunityId).deepClone();
		 }
		 otmList.add(otm);
		 otmMap.put(otm.OpportunityId, otmList);
	}

	//Now here goes your logic, I have logically copied from the code given by you
	for (Opportunity opp : Trigger.NewMap.values()) {
			  Integer comm =0;
			  Integer otmcount = otmMap.get(opp.Id).size();
			  system.debug('otmcount:'+otmcount);
			  splits__c spl4oppowner = new splits__c();
			  spl4oppowner.opportunity__c = opp.id; 
			  spl4oppowner.user__c = opp.ownerid; 
			  spl4oppowner.Split__c = (100/(otmcount+1));
			  insert spl4oppowner;
		 for (OpportunityTeamMember otm1 : otmMap.get(opp.Id)) {
			  comm =  (otmcount);
			  splits__c spl = new splits__c();
			  spl.opportunity__c = opp.id;
			  spl.user__c = otm1.userid;
			  spl.Split__c = (100/(comm+1));
			  spl1.add(spl);
		 }
		 insert spl1;
	}
}

 Please let me know if you are facing any issues.

 

Regards,

Lakshman