+ Start a Discussion
Tyler ZikaTyler Zika 

return the selected value of a picklist

Hello,

From this documentation https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_PicklistEntry.htm#apex_Schema_PicklistEntry_getLabel

I see how how to return values from a custom picklist value. I've implented to code in my Visualforce controller like so.
 
public List<PicklistEntry> transportation
{
        get
        {
          Schema.DescribeFieldResult F = GO_Event__c.Transportation__c.getDescribe();
          List<Schema.PicklistEntry> P = F.getPicklistValues();
          
          return P;
        }
}


The problem is it's returning all the values instead and other code instead of just the one selected value in the record. Here is the Visualforce code

<li>{!transportation}</li>
and a screen shot of the output on the page.
User-added image

How to I get the one selected picklist value?
Best Answer chosen by Tyler Zika
Abhishek BansalAbhishek Bansal

Hi Tyler,
 
There is no impact of the data type. I think the field is not visible for the logged in user from which you are viewing the page.
Please check the FLS of the "Transportation__c" field.

Thanks,
Abhishek Bansal

All Answers

Abhishek BansalAbhishek Bansal
Hi Tyler,

Please use the below code :
public List<String> transportation
{
        get
        {
          Schema.DescribeFieldResult F = GO_Event__c.Transportation__c.getDescribe();
          List<Schema.PicklistEntry> P = F.getPicklistValues();
          List<String> picklistValues = new List<String>();
		  for(Schema.PicklistEntry pickValue : p){
			  picklistValues.add(pickValue.getValue());
		  }
          return picklistValues;
        }
}


Please let me know if you need more help or information on this.

Thanks,
Abhishek Bansal

Tyler ZikaTyler Zika
Hey thanks for your input. I see this returns ALL the values from my picklist. But what I want is the return value, the selected value from the record. In my record "Cruise Ship" is the selected transportation. How do I make that value print on the VF page?
Tyler ZikaTyler Zika
Here is the controller code for my VF page.
public GO_Event__c GoEvent { get; set; }

 private void init()
    {
        currentUserContactId = [select COntactId from User where Id = :userinfo.getUserId()].COntactId;
        IsAlreadyRegistered = false;
        GoEvent = [select Id, name, Event_Day_of_the_Week__c, Return_Address__c, Return_Time__c, Departure_Time__c, Departure_Address__c, Event_Date__c, Event_Description__c, Event_Description_Short__c, Registrations__c, Event_Amount__c, Event_GO_Region__c, Event_Image_URL__c, Spots_Available__c, Event_Publish_Date__c, Event_Time__c, Event_Time_Start__c, Event_Time_End__c, Event_Status__c, Event_Type__c, Event_Coordinator__c, Event_Coordinator_Email__c, Transportation__c, Public_Page_URL__c from GO_Event__c where Id = :eventId];
}

The API name in the object is called Transportation__c, which is in the GoEvent SOQL call.

When I type
{!GoEvent.Transportation__c}
in my VF page, it doesn't return anything, but works for all the other values list in the GoEvent. I think the problem is because it's a picklist.
 
Abhishek BansalAbhishek Bansal
Hi Tyler,

You are going in the wrong direction if you want to show the value of the Picklist from your record.
In order to show the picklist value of a single record you have to do something as mentioned below :
public String getTransportationValue {
	GO_Event__c tempGoEvent = [Select Transportation__c from GO_Event__c LIMIT 1];
	return tempGoEvent.Transportation__c;
}
//On your VF page use the below statement 
{!transportationValue}

Please let me know if you need more help on this.

Thanks,
Abhishek Bansal
Abhishek BansalAbhishek Bansal

Hi Tyler,
 
There is no impact of the data type. I think the field is not visible for the logged in user from which you are viewing the page.
Please check the FLS of the "Transportation__c" field.

Thanks,
Abhishek Bansal

This was selected as the best answer
Tyler ZikaTyler Zika
That was it! The FLS. Thank you!!!