I have a VF page & controller that is querying accounts and putting them into a list, and them displaying the fields Account Name, State,and Last Activity.

I have a custom setting that is a list of zip codes (which is also a field on account) and their corresponding avg per capita income.

I am able to get values from custom settings, but have trouble understanding iterating them to match the account data displayed.

Is using a map the best way to accomplish this?


Since both your account list and your custom settings list have the zip codes you could use that as the key in a map. Query your custom settings and put them in a map similar to this example.

Map<String, Double> zipToCapitaMap = new Map<String, Double>(); //Assuming field types

for(Account a :myListOfAccounts){
    Double avgPerCapita = zipToCapitaMap.get(a.zipcode);
    String myAccountInfo = 'Name '+a.Name+' State '+a.State+' AvgPerCapita '+ avgPerCapita; 

Double check that your zip codes in the custom settings are exactly as they are on the accounts. No extra white space or quotes.

  • marking as the best answer as my custom setting is of the List variety Feb 26 '15 at 15:14

Another way is to access the custom setting on the page directly and use one of the Salesforce Functions for compare for example:

<apex:repeat value="{!accounts}" var="a">
    <apex:outputText value="{!a.name}" 
                     rendered="{!CONTAINS(!$Setup.SettingsObject__c.SETTING__c, a.PostalCode})}" />

Used $Setup global variable here:

A global merge field type to use when referencing a custom setting of type “hierarchy.”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.