You need to sign in to do that
Don't have an account?
David 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;
Try following code.
All Answers
yes there is simple way to do that which will execute a single SOQL
use it in following way
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.
Map you cna try like below
Please mark this as solution if this will help you
Try following code.
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);
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
Thanks,
Himanshu
Thank you again.
Yes, you can do it as well in simple way.
I have just showed you path to make the test class, you can modify it
Thanks,
Himanshu
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.
I will be glad to help you.
Thanks,
Himanshu