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

Some help bulkifying a trigger
Hi folks,
Today I come to you looking for some help understanding how to bulkify a trigger. I currently have this code.
if(Trigger.isUpdate)
{
Projection_Multi_Year__c testObj = Trigger.new[0];
List<Projection_Multi_Year__c> checkForDups = new List <Projection_Multi_Year__c>([select id from Projection_Multi_Year__c where
account__c = :testObj.account__c and ownerID = :testObj.ownerID]);
if(checkForDups.size() != 0)
{
trigger.new[0].addError('Update dupe error');
}
else
{
String profileID = UserInfo.getProfileID();
String profileName = [Select name from Profile where ID = :profileID].name;
for(Projection_Multi_Year__c obj : Trigger.New)
{
String accountName = [select name from Account where ID = :obj.Account__c].name;
User ownerObj = [select ID,name,LHH_Zone__c,Region__c,ManagerID from User where ID = :obj.ownerID];
String ownerName = [select name from User where ID = :obj.ownerID].name;
String managerName = [select name from USER where ID = :ownerObj.ManagerID].name;
System.debug('obj.CreatedBy.name = ' + old.CreatedBy.Name);
System.debug('obj.CreatedBy = ' + old.CreatedByID);
obj.Projection_Name__c = accountName + ' - ' + divisionName + ' - ' + ownerObj.name;
//this ^ field has 32,000 some chars. Need to also make a var for a 80 char field
if(accountName.length() > 80)
{
accountName = accountName.substring(0,79);
}
if(ownerName.length() > 80)
{
ownerName = ownerName.substring(0,79);
}
String truncatedName = ownerName + ' - ' + accountName + ' - ' + divisionName;
if(truncatedName.length() > 80)
{
truncatedName = truncatedName.substring(0,79);
}
obj.Name = truncatedName;
obj.Manager__c = managerName;
obj.LHH_Zone__c = ownerObj.LHH_Zone__c;
obj.Country_Region__c = ownerObj.Region__c;
}//end of For
So while I understand the basics of my problem I am not sure how to write the solution. I am going to be sending a bulk upload of thousnads of records at this trigger, so I cant have queries inside of a for loop that will itereate through all the records.
Help would be much appriciated on the specifics.
I am working on a solution to this now. Please dont waste time trying to solve it for me. I will post the solution later today.
Hi
Am sure you wud work out a solution for this...
Just in case this helps you out...A tip for bulkifying triggers is
'Do not fire SOQL queries inside for loop'. This leads to hitting the governer limits on the number of queries fired from a context during bulk operations( insert/update/delete).
Move these queries outside the for loop.
String accountName = [select name from Account where ID = :obj.Account__c].name;
User ownerObj = [select ID,name,LHH_Zone__c,Region__c,ManagerID from User where ID = :obj.ownerID];
String ownerName = [select name from User where ID = :obj.ownerID].name;
String managerName = [select name from USER where ID = :ownerObj.ManagerID].name;
Create Maps to maintain appropriate relationships between the queried results and the Ids of the records for which the trigger is invoked..
Inside the for loop perform the logic required using the data in the Maps.
Hi,
To bulkify and stay in salesforce limit firstly you need to remove all soql queries form the for loop.
To builkifying you need to use data structures like map,set,list etc.
I hope below code snippet will help you in that.
Regards,
Purushottam
Purushottam Bhaigade Software Developer, Screen-Magic
Mobile: +91(957) 981-1985
purushottambhaigade@gmail.com | www.screen-magic.com
Contact me: