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
Ganesh BollinaGanesh Bollina 

Populate complete state value in a visualforce page if abbreviation is fetched in a query?

I need to auto populate complete state value in a visualforce page if it fetches state codes like oh,FL with the help of apex?
Best Answer chosen by Ganesh Bollina
Balayesu ChilakalapudiBalayesu Chilakalapudi
Use a Map to hold all your state codes and full forms like this,
Map<String,String> statemap=new Map<String,String>();
statemap.put('FL','FinLand');
Query the states and iterate over a loop like this ( I assumed your object name as Contact, replace it with your Object)
List<Contact> statelist=[SELECT state FROM Contact];
for(Contact c:statelist){
   if(statemsp.containsKey(c.state)){
      c.state=statemap.get(c.state);
   }
}
display state value in vf page like this
{!contact.state}
Let me know if it helps

All Answers

James LoghryJames Loghry
The best way to do this currently, in my opinion, is to create a custom setting (or custom meta data) and then reference it in Apex.
  1. Create a list custom setting, with a field called Value.
  2. Populate the custom setting, using the two character version as the Name, and the full version as Value__c.
  3. Then in Apex, you'd use something like:
    1. String fullState = State_Custom_Setting__c.getValues(myTwoCharValue.toUpperCase()).Value__c;
Balayesu ChilakalapudiBalayesu Chilakalapudi
Use a Map to hold all your state codes and full forms like this,
Map<String,String> statemap=new Map<String,String>();
statemap.put('FL','FinLand');
Query the states and iterate over a loop like this ( I assumed your object name as Contact, replace it with your Object)
List<Contact> statelist=[SELECT state FROM Contact];
for(Contact c:statelist){
   if(statemsp.containsKey(c.state)){
      c.state=statemap.get(c.state);
   }
}
display state value in vf page like this
{!contact.state}
Let me know if it helps
This was selected as the best answer