+ Start a Discussion
Ckevin1984Ckevin1984 

Regarding Bulk Trigger

I have written a trigger and try to change the trigger to bulk trigger version. Can anyone help me, i feel confused about bulk trigger. You can just make some change in one recordTypeId and post back as a reply. Thank you very much in advance. Here is my whole trigger looks like.

 

trigger TimeSheetTrigger on Timesheet__c (after delete, after insert,after update)
{
double sumTotalHours = 0.0;

Campaign[] sheetsToUpdateCampaign = new Campaign[]{};
Case[] sheetsToUpdateCase = new Case[]{};
Contract[] sheetsToUpdateContract = new Contract[]{};
Job__c[] sheetsToUpdateJob = new Job__c[]{};
Opportunity[] sheetsToUpdateOpportunity = new Opportunity[]{};
Project__c [] sheetsToUpdateProject = new Project__c[]{};


//***********************************************
//Code for updating existing records and new records
//***********************************************

if(Trigger.isInsert)
{
Timesheet__c [] teNew = trigger.new;

for(Timesheet__c te : teNew)
{
//The selected RecordType is Campaign
if(te.RecordTypeId == '012200000001Xyp')
{
for (Campaign timesheet : [select Id,Name, Hours__c from Campaign ])
{
//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Campaign__c = :timesheet.Id ])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateCampaign.add(timesheet);
update sheetsToUpdateCampaign;
}
}

//The selected RecordType is Case
if(te.RecordTypeId == '012200000001X36')
{
for (Case timesheet : [select Id, Hours__c from Case ])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Case__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateCase.add(timesheet);
update sheetsToUpdateCase;
}
}
//The selected RecordType is Contract
if(te.RecordTypeId == '012200000001Xyk')
{
for (Contract timesheet : [select Id,Name, Total_Contract_Hours__c from Contract ])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Contract__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Total_Contract_Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateContract.add(timesheet);
update sheetsToUpdateContract;
}
}
//The selected RecordType is Job
if(te.RecordTypeId == '012200000001X3B')
{
for (Job__c timesheet : [select Id,Name, Hours__c from Job__c ])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Job__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateJob.add(timesheet);
update sheetsToUpdateJob;
}
}

//The selected RecordType is Opportunity
if(te.RecordTypeId == '012200000001YBG')
{
for (Opportunity timesheet : [select Id,Name, Hours__c from Opportunity ])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Opportunity__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateOpportunity.add(timesheet);
update sheetsToUpdateOpportunity;
}
}

//The selected RecordType is Project
if(te.RecordTypeId == '012200000001X31')
{
for (Project__c timesheet : [select Id,Name, Hours__c from Project__c ])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Project__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdateProject.add(timesheet);
update sheetsToUpdateProject;
}
}
}


}

//***********************************************
//Code for updating when a record is updated
//***********************************************

else if(Trigger.isUpdate)
{
//sum total both old and new
Timesheet__c [] oldTime = Trigger.old;
Timesheet__c [] newTime = Trigger.new;
Double newSum = 0.0;
Double oldSum = 0.0;

for(Timesheet__c newTe: newTime)
{
for(Timesheet__c oldTe : oldTime)
{

//Selected RecordType is Campaign
if(newTe.RecordTypeId == '012200000001Xyp')
{
Campaign oldTimesheet = [Select Id, Name, Hours__c from Campaign where id = :oldTe.Campaign__c];
Campaign newTimesheet = [Select Id, Name, Hours__c from Campaign where id = :newTe.Campaign__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Campaign__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Campaign__c = :newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;

}
oldTimesheet.Hours__c= oldSum;
newTimesheet.Hours__c = newSum;

sheetsToUpdateCampaign.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateCampaign.add(oldTimesheet);
}
}

//Selected RecordType is Case
if(newTe.RecordTypeId == '012200000001X36')
{
Case oldTimesheet = [Select Id, Hours__c from Case where id = :oldTe.Case__c];
Case newTimesheet = [Select Id, Hours__c from Case where id = :newTe.Case__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Case__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Case__c = :newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;
}
oldTimesheet.Hours__c= oldSum;
newTimesheet.Hours__c = newSum;

sheetsToUpdateCase.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateCase.add(oldTimesheet);
}
}

//Selected RecordType is Contract
if(newTe.RecordTypeId == '012200000001Xyk')
{
Contract oldTimesheet = [Select Id, Name, Total_Contract_Hours__c from Contract where id = :oldTe.Contract__c];
Contract newTimesheet = [Select Id, Name, Total_Contract_Hours__c from Contract where id = :newTe.Contract__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Contract__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Contract__c = :newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;

}
oldTimesheet.Total_Contract_Hours__c= oldSum;
newTimesheet.Total_Contract_Hours__c = newSum;

sheetsToUpdateContract.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateContract.add(oldTimesheet);
}
}

//Selected RecordType is Job
if(newTe.RecordTypeId == '012200000001X3B')
{
Job__c oldTimesheet = [Select Id, Name, Hours__c from Job__c where id = :oldTe.Job__c];
Job__c newTimesheet = [Select Id, Name, Hours__c from Job__c where id = :newTe.Job__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Job__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Job__c = :newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;

}
oldTimesheet.Hours__c= oldSum;
newTimesheet.Hours__c = newSum;

sheetsToUpdateJob.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateJob.add(oldTimesheet);
}
}
//Selected RecordType is Opportunity
if(newTe.RecordTypeId == '012200000001YBG')
{
Opportunity oldTimesheet = [Select Id, Name, Hours__c from Opportunity where id = :oldTe.Opportunity__c];
Opportunity newTimesheet = [Select Id, Name, Hours__c from Opportunity where id = :newTe.Opportunity__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Opportunity__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Opportunity__c =

:newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;

}
oldTimesheet.Hours__c= oldSum;
newTimesheet.Hours__c = newSum;

sheetsToUpdateOpportunity.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateOpportunity.add(oldTimesheet);
}
}

//Selected RecordType is Project
if(newTe.RecordTypeId == '012200000001X31')
{
Project__c oldTimesheet = [Select Id, Name, Hours__c from Project__c where id = :oldTe.Project__c];
Project__c newTimesheet = [Select Id, Name, Hours__c from Project__c where id = :newTe.Project__c];

Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Project__c = :oldTimesheet.Id];
Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Project__c = :newTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;

}
oldTimesheet.Hours__c= oldSum;
newTimesheet.Hours__c = newSum;

sheetsToUpdateProject.add(newTimesheet);
if(newTimesheet.Id != oldTimesheet.Id)
{
sheetsToUpdateProject.add(oldTimesheet);
}
}

}
}

//commit the changes to Salesforce
if(newTime[0].RecordTypeId == '012200000001Xyp')
update sheetsToUpdateCampaign;
if(newTime[0].RecordTypeId == '012200000001X36')
update sheetsToUpdateCase;
if(newTime[0].RecordTypeId == '012200000001Xyk')
update sheetsToUpdateContract;
if(newTime[0].RecordTypeId == '012200000001X3B')
update sheetsToUpdateJob;
if(newTime[0].RecordTypeId == '012200000001YBG')
update sheetsToUpdateOpportunity;
if(newTime[0].RecordTypeId == '012200000001X31')
update sheetsToUpdateProject;
}

//***********************************************
//Code for updating when a record is deleted
//***********************************************

else if(Trigger.isDelete)
{

Timesheet__c [] teOld = trigger.old;

for(Timesheet__c te: teOld)
{
if(te.RecordTypeId == '012200000001Xyp')
{
for (Campaign timesheet: [select Id, Name, Hours__c from Campaign])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Campaign__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

sheetsToUpdateCampaign.add(timesheet);
}
}

if(te.RecordTypeId == '012200000001X36')
{
for (Case timesheet: [select Id, Hours__c from Case])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Case__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

sheetsToUpdateCase.add(timesheet);
}
}

if(te.RecordTypeId == '012200000001Xyk')
{
for (Contract timesheet: [select Id, Name, Total_Contract_Hours__c from Contract])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Contract__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Total_Contract_Hours__c = sumTotalHours;

sheetsToUpdateContract.add(timesheet);
}
}

if(te.RecordTypeId == '012200000001X3B')
{
for (Job__c timesheet: [select Id, Name, Hours__c from Job__c])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Job__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

sheetsToUpdateJob.add(timesheet);
}
}

if(te.RecordTypeId == '012200000001YBG')
{
for (Opportunity timesheet: [select Id, Name, Hours__c from Opportunity])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Opportunity__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

sheetsToUpdateOpportunity.add(timesheet);
}
}

if(te.RecordTypeId == '012200000001X31')
{
for (Project__c timesheet: [select Id, Name, Hours__c from Project__c])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Project__c = :timesheet.Id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Hours__c = sumTotalHours;

sheetsToUpdateProject.add(timesheet);
}
}

}

//commit the changes to Salesforce
if(teOld[0].RecordTypeId == '012200000001Xyp')
update sheetsToUpdateCampaign;
if(teOld[0].RecordTypeId == '012200000001X36')
update sheetsToUpdateCase;
if(teOld[0].RecordTypeId == '012200000001Xyk')
update sheetsToUpdateContract;
if(teOld[0].RecordTypeId == '012200000001X3B')
update sheetsToUpdateJob;
if(teOld[0].RecordTypeId == '012200000001YBG')
update sheetsToUpdateOpportunity;
if(teOld[0].RecordTypeId == '012200000001X31')
update sheetsToUpdateProject;
}     
}

Best Answer chosen by Admin (Salesforce Developers) 
iBr0theriBr0ther

Take all select statements, insert, update out of the loop or you will hit the sf limitation. put the result in list or map then loop through it instead. put all records to be updated in list or map and then insert/update outside the loop.

 

Cheers,

All Answers

iBr0theriBr0ther

Take all select statements, insert, update out of the loop or you will hit the sf limitation. put the result in list or map then loop through it instead. put all records to be updated in list or map and then insert/update outside the loop.

 

Cheers,

This was selected as the best answer
ahab1372ahab1372

I haven't gone through all the code, but if the objects are child objects of the timesheet__c, why not use RollUp Summary fields?