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
cathy369cathy369 

efficiency question

while my tag is not "newbie" any longer, i am not a developer... in particular, not an apex, java or any OOP developer so am greatly in need of advice (most of the time!!).... 

 

anyways, i have a custom object, RunCharges, that contains 230 records.  depending on value of some fields on custom object, i need to access up to 5 of these records and associated values in a trigger, potentially up to 20 times... 

 

is it more efficient to do create a LIST, perform one SELECT for all records and iterate thru this list up to 20 times, or to do a SELECT statement with the specific criteria, i.e. process and qty range (causing a potential of 20 SELECT statements)??

 

or, i could do 5 SELECT statements for each type of process, and iterate through the 20 or so records until i find my quantity range? 

 

any advice is more than welcoome... thanks very much.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

A single query is most effecient. If possible, consider using a MAP instead of a LIST. You can use one field as a "key" (or even a composite key... meaning using string math to create unique entries). As an arbirary example, say you had two fields with a city and state, and a third field that contained a tax rate:

 

Map<String,Tax_Rate__c> taxes = new Map<String,Tax_Rate__c>();
for(Tax_Rate__c tr:[select id,State__c,City__c,Rate__c from tax_rate__c])
  taxes.put(tr.city__c+'/'+tr.state__c,tr);

 From here, you could then look up a city and state that's loaded in memory. You could use a similar method with your data if you can figure out a way to map your data uniquely. It's also possible to put maps inside of maps, if that makes it easier to visualize/code.

All Answers

sfdcfoxsfdcfox

A single query is most effecient. If possible, consider using a MAP instead of a LIST. You can use one field as a "key" (or even a composite key... meaning using string math to create unique entries). As an arbirary example, say you had two fields with a city and state, and a third field that contained a tax rate:

 

Map<String,Tax_Rate__c> taxes = new Map<String,Tax_Rate__c>();
for(Tax_Rate__c tr:[select id,State__c,City__c,Rate__c from tax_rate__c])
  taxes.put(tr.city__c+'/'+tr.state__c,tr);

 From here, you could then look up a city and state that's loaded in memory. You could use a similar method with your data if you can figure out a way to map your data uniquely. It's also possible to put maps inside of maps, if that makes it easier to visualize/code.

This was selected as the best answer
cathy369cathy369

thanks very much for your input... very good idea to create the unique key....thanks again...c