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
David Durant 5David Durant 5 

Bulkify Trigger List Statement Code

I am wondering if there is a better way for me to capture all of of the ID's I need instead of in indivudial list statements. Example below: 
list<Utility__c>  LADWPId           = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Los Angeles' Limit 1]);
    list<Utility__c>  ARMWCId           = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Adams Ranch Mutual Water Company' Limit 1]);
    list<Utility__c>  AMWCId            = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Amarillo' Limit 1]);
    list<Utility__c>  ANAHEIMId         = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Anaheim, City of' Limit 1]);
    list<Utility__c>  ARCADIAId         = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Arcadia, City of' Limit 1]);
    list<Utility__c>  AZUSATHREEId      = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Azusa (Three Valleys)' Limit 1]);
    list<Utility__c>  AZUSAUPPERId      = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Azusa' Limit 1]);
    list<Utility__c>  BELLFLOWERHGWCId  = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Bellflower' Limit 1]);
    list<Utility__c>  BELLFLOWERSMWCId  = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Bellflower-Somerset' Limit 1]);
    list<Utility__c>  BELLGARDENSId     = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Bell Gardens, City of' Limit 1]);
    list<Utility__c>  BERLYWOODId       = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Berylwood Heights' Limit 1]);
    list<Utility__c>  BEVERLYHILLSId    = new list<Utility__c>([SELECT Id FROM Utility__c WHERE Name='Beverly Hills, City of' Limit 1]);
Basically, I have a picklist on the opportunity page layout that holds about 240 values and I have a custom object with the same exact 240 values but ovbiously also holds more information about the individual cities.  I am inserting the lookup value into the page layout corresponding with the picklist value. For example, Picklist = Berlywood Heights, Lookup Field = Berlywood Heights.  I am going through in the trigger and capturing all the ID's I need and then inserting them into individual if, else if statements. The trigger is working great so far and I have coded about 60 of the cities in. I am wondering though if there is a way to in esence condense the code. Below is an example of how I am attaching the ID's to the if statment:
 
else if (o.Water_Agency__c == 'Berylwood Heights') {
              o.Organization_Name__c = BERLYWOODId[0].Id;

 
Best Answer chosen by David Durant 5
Himanshu ParasharHimanshu Parashar
Hi David,

Try following code.
 
trigger UpdateUtility on Opportunity (before insert) 
{

	// Please add all Name in below set
	set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

	Map<String,Utility__c> UtilityMap = new Map<String,Utility__c>();

	for(Utility__c ut : [SELECT Id,name FROM Utility__c WHERE Name in :strNames ] ) 
	{
		UtilityMap.put(ut.name,ut);
	}



    for (Opportunity o : Trigger.New) 
	{
		if (UtilityMap.containskey(o.Water_Agency__c)) 
		{

              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c).id;
		}
    }
}

 

All Answers

Himanshu ParasharHimanshu Parashar
Hi David,

yes there is simple way to do that which will execute a single SOQL
 
set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company'};
Map<String,Id> UtilityMap = new Map<String,Id>();
//Prepare your Map with String and value
for(Utility__c utility : [[SELECT Id,name FROM Utility__c WHERE Name in : strNames];
{
    UtilityMap.put(utility.name,utility.id);
}


use it in following way
if (UtilityMap.containskey(o.Water_Agency__c)) {
              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c);

I don't know how your trigger code is executing but I guess you can optimize that as well by using above lines


Makes sense?


Thanks,
Himanshu
Salesforce Certified Developer, Administrator, Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

Map<String,Utility__c> UtilityMap = new Map<String,Utility__c>();
//Prepare your Map with String and value

for(Utility__c utility : [[SELECT Id,name FROM Utility__c WHERE Name in :strNames ];
{
    UtilityMap.put(utility.name,utility);
}

Map  you cna try like below 
 
if (UtilityMap.containskey(o.Water_Agency__c)) {
              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c).id;



 
David Durant 5David Durant 5
Thank you both for your replies.  I will go ahead and try them. Just out of curiosity just to get the trigger up and running will I run into any issues having 240 list statements and 240 if/else if statements?  I know it is not scalable but will I run into any issues other than scalability?
Himanshu ParasharHimanshu Parashar
You will hit SOQL limit which is 100
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
trigger UpdateUtility on Opportunity (before insert) 
{

	// Please add all Name in below set
	set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

	Map<String,Utility__c> UtilityMap = new Map<String,Utility__c>();

	for(Utility__c utility : [[SELECT Id,name FROM Utility__c WHERE Name in :strNames ];
	{
		UtilityMap.put(utility.name,utility);
	}



    for (Opportunity o : Trigger.New) 
	{
		if (UtilityMap.containskey(o.Water_Agency__c)) 
		{

              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c).id;
		}
    }
}

Please mark this as solution if this will help you 
 
David Durant 5David Durant 5
I am receiving an error:  Compile Error: Variable does not exist: utility.name at line 11 column 24
David Durant 5David Durant 5
I'm still receiving the same error.

User-added image
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger UpdateUtility on Opportunity (before insert) 
{

	// Please add all Name in below set
	set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

	Map<String,Utility__c> UtilityMap = new Map<String,Utility__c>();

	for(Utility__c utility : [SELECT Id,name FROM Utility__c WHERE Name in :strNames ] ) 
	{
		UtilityMap.put(utility.name,utility);
	}



    for (Opportunity o : Trigger.New) 
	{
		if (UtilityMap.containskey(o.Water_Agency__c)) 
		{

              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c).id;
		}
    }
}


 
Himanshu ParasharHimanshu Parashar
Hi David,

Try following code.
 
trigger UpdateUtility on Opportunity (before insert) 
{

	// Please add all Name in below set
	set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

	Map<String,Utility__c> UtilityMap = new Map<String,Utility__c>();

	for(Utility__c ut : [SELECT Id,name FROM Utility__c WHERE Name in :strNames ] ) 
	{
		UtilityMap.put(ut.name,ut);
	}



    for (Opportunity o : Trigger.New) 
	{
		if (UtilityMap.containskey(o.Water_Agency__c)) 
		{

              o.Organization_Name__c = UtilityMap.get(o.Water_Agency__c).id;
		}
    }
}

 
This was selected as the best answer
David Durant 5David Durant 5
It worked. Thank you! 

Just so I understand do you mind telling me why it was throwing an error? Also, what are we actually saying inside the parenthesis next to UtilityMap.put(Utility.name,utility);
Himanshu ParasharHimanshu Parashar
as far as I can tell, you might have a apex class names Utility ?
Amit Chaudhary 8Amit Chaudhary 8
My Mistakly i have added the ; after loop. My mistak

for(Utility__c ut : [SELECT Id,name FROM Utility__c WHERE Name in :strNames ] );

And UtilityMap will be your key value par with "Utility__c" Name and its Object.

Please mark above post as solution if that will help you. So that if any one has same issue this post can help



 
David Durant 5David Durant 5
I do not. I understand in your code that the ut is the variable name but what is the reason for the .name,ut?
Himanshu ParasharHimanshu Parashar
If that is not the case then as Amit mentioned and I can see as well that he had a ; at the end of the statement by mistake which I removed in my code so now if you will rename that again back to utility it should work.

Thanks,
Himanshu

 
David Durant 5David Durant 5
Okay I understand now.  Is there a better way to do the test class then inserting an opportunity record for each utility?

Thank you again.
Himanshu ParasharHimanshu Parashar
Hi David,

Yes, you can do it as well in simple way.

 
Account acc= new Account(name='xx');
insert acc;
set<String> strNames = new set<String>{'Los Angeles','Los Angeles','Adams Ranch Mutual Water Company','Amarillo','Anaheim, City of','Arcadia, City of','Azusa (Three Valleys)','Azusa','Bellflower','Bellflower-Somerset','Bell Gardens, City of','Berylwood Heights','Beverly Hills, City of'};

List<Opportunity> lstOpp = new List<Opportunity>();

for(String str : strNames)
{
   Opportunity opp = new Opportunity(Water_Agency__c=str,name=str,closedate=system.today(),stagename='xx',Accountid=acc.id);
lstOpp.add(opp);
}

insert lstOpp;
I have just showed you path to make the test class, you can modify it

Thanks,
Himanshu
David Durant 5David Durant 5
Thank you again! I am running the test right now.

Would either of you be interested teaching me once or twice a week. There would ovbiously be compensation involved. I would love to continue to learn more in development. 
Himanshu ParasharHimanshu Parashar
HI David,

I will be glad to help you.

Thanks,
Himanshu