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
BerettaJonBerettaJon 

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.

BerettaJonBerettaJon

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.

GsajeeshGsajeesh

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.

 

purupuru

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.

 

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;
		List<Projection_Multi_Year__c> pMutliYearList = Trigger.new;

		//For account
		Map<Projection_Multi_Year__c, String> pMutliYearAccountNameMap = new Map<Projection_Multi_Year__c, String>();
		Map<Projection_Multi_Year__c, Id> pMutliYearAccountIdMap = new Map<Projection_Multi_Year__c, Id>();

		//For User
		Map<Projection_Multi_Year__c, Id> pMutliYearOwnerMap = new Map<Projection_Multi_Year__c, Id>();
		Map<Projection_Multi_Year__c, String> pMutliYearOwnerNameMap = new Map<Projection_Multi_Year__c, String>();

		//For Manager
		Map<Projection_Multi_Year__c, String> pMutliYearManagerNameMap = new Map<Projection_Multi_Year__c, String>();

		// create map for Projection_Multi_Year__c and account Id, Projection_Multi_Year__c and User Id, 
		for(Projection_Multi_Year__c obj : pMutliYearList){
			pMutliYearAccountIdMap.put(obj, obj.Account__c);
			pMutliYearOwnerMap.put(obj,obj.ownerID);
		}


		// fetch all accounts
		Map<Id,Account> accMap = new Map<Id,Account>([select Id, name from Account where ID in :pMutliYearAccountIdMap.values()]);

		// create map for Projection_Multi_Year__c and account Name
		for(Projection_Multi_Year__c obj : pMutliYearAccountIdMap.keySet()){
			Id accId = pMutliYearAccountIdMap.get(obj);
			String accName = accMap.get(accId).Name;
			if(accName.length() > 80){
				accName = accName.substring(0,79);
			}
			pMutliYearAccountNameMap.put(obj, accName);
		}


		// fetch all Users
		Map<Id,User> userMap = new Map<Id,Account>([select ID,name,LHH_Zone__c,Region__c,ManagerID from User where ID in :pMutliYearOwnerMap.values()]);

		// create map for Projection_Multi_Year__c and Owner Name, Projection_Multi_Year__c and Manager Name
		for(Projection_Multi_Year__c obj : pMutliYearOwnerMap.keySet()){
			Id userId = pMutliYearOwnerMap.get(obj);
			String ownerName = userMap.get(userId).Name;
			if(ownerName.length() > 80){
				ownerName = ownerName.substring(0,79);
			}
			pMutliYearOwnerNameMap.put(obj, ownerName);
			pMutliYearManagerNameMap.put(obj,userMap.get(userId).ManagerID.Name);
		}


		//update the values of Projection_Multi_Year__c
		for(Projection_Multi_Year__c obj : pMutliYearList){
			String accountName = pMutliYearAccountNameMap.get(obj);
			String ownerName = pMutliYearOwnerNameMap.get(obj);
			String managerName = pMutliYearManagerNameMap.get(obj);

			obj.Projection_Name__c = accountName + ' - ' + divisionName + ' - ' + ownerName;
			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 = userMap.get(pMutliYearOwnerMap.get(obj)).LHH_Zone__c;
			obj.Country_Region__c = userMap.get(pMutliYearOwnerMap.get(obj)).Region__c;
		}
	}//end of else
}//end of if

Regards,

Purushottam

 

Purushottam Bhaigade Software Developer, Screen-Magic
Mobile: +91(957) 981-1985
purushottambhaigade@gmail.com | www.screen-magic.com

 

Facebook Twitter LinkedIn
Contact me: Skype purushottam8688