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
sonali  vermasonali verma 

picklist value not showing when page loads

Hi
I have a custom setting defined as Countries__c which has field  CountryCode__c. so I store as

United States Of Ameria    US
Canada                             CA
Congo                               CG

I have custom object C1__c which has a field called  country_region__c.

In browser URL I will append the ID of the C1 object and fetch the values in fields in visual force page

assume in record we have   country_region__c=United States Of Ameria 

I keep debug statement , value comes in C1Country_Region  property but it doesnt show country Name in picklist in visual force page.
public class C1Controller {

     public String C1Country_Region { get; set; }

     public String C1ID{get;set;}

    public C1__c   c1{get;set;}

   private List<SelectOption> countryCodeList = new List<SelectOption>();

   public String selectedCountryCode {get; set;}

   public C1Controller()

    {

        C1ID=ApexPages.currentpage().getparameters().get('id');

                
        if(String.isBlank(C1ID))

        {

                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Id.');

                ApexPages.addMessage(myMsg);

                return;

        }

        else

        {

          try

          {

            fetchC1values();

          }

          catch(System.QueryException ex){
                 
              Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'No Record'));

          }

}

public List<SelectOption> getCountryCodes(){

    if(countryCodeList.isEmpty()){

        countryCodeList.add(new SelectOption("None", "--Select--"));

        for(Countries__c country :Countries__c.getAll().values()){

            countryCodeList.add(new SelectOption(country.CountryCode__c, country.Name));

        }

    }

    return countryCodeList;

}

  public void fetchC1values()

    {

         c1 = new C1__c();

          c1=[SELECT id, Country_Region__c

             FROM C1__c
           WHERE Encrypted_ID__c=:C1ID];

         if (c1 != NULL)

         {

             
       if(!string.isBlank(c1.Country_Region__c))

       {

          system.debug('country'+c1.Country_Region__c);

          C1Country_Region =c1.Country_Region__c;

         }
}

 In visual force page

 <apex:selectList value="{!C1Country_Region}" size="1">

 
               <apex:selectOptions value="{!countryCodes}"></apex:selectOptions>

                                                 
  </apex:selectList>

also if I select UnitedState of america from picklist then the same should be stored in the sobject.
any help will be grealty helpful

thanks
sonali
Best Answer chosen by sonali verma
Shruti SShruti S
User-added image
As I have shown in the figure, please confirm whether you are storing IN or India in the field on the Object.  You have to store the value of the selected option of the picklist in Visualforce page onto the field of the Object. 
Only if you store the value on the field on the object, you will be able to automatically display that selected value in the picklist on the Visualforce page.

All Answers

Shruti SShruti S
User-added image
As I have shown in the figure, please confirm whether you are storing IN or India in the field on the Object.  You have to store the value of the selected option of the picklist in Visualforce page onto the field of the Object. 
Only if you store the value on the field on the object, you will be able to automatically display that selected value in the picklist on the Visualforce page.
This was selected as the best answer
sonali  vermasonali verma
Hi shruti
I want to store India in the field of sobject.   But IN is getting stored in the field of sobject.

Thanks
sonali
sonali  vermasonali verma
Hi shruti
Pls ignore my previous post. I think I was wrong. 
The code is working fine.

thanks very much

sonali