I am new to SFDC and started development in Apex and Visualforce Page with tutorials and online samples.
So, here what I have tried and got stuck at the point.
What I am trying to achieve:
1. In VF page, I want to show Custom field's values of Picklist data type in tabular format (In one of the coulmn) and it should be as Outputfield but not as a Inputfield.
2. In VF page, I want to access Custom field's values of Picklist data type of my Custom Object in tabular format (In one of the coulmn) and it should be as Outputfield but not as a Inputfield.
What I have tried to achieve 1st use case:
- In Contact custom fields, I have created new Custom Picklist "interested_technologies__c" and there was already custom field "Level__c" of picklist data type.
- I have created VF ("CustomerSearch") and Apex class ("ContactSearchController") as follows:
VF Page – CustomerSearch
<apex:page controller="ContactSearchController" sidebar="false">
<apex:form >
<apex:pageBlock mode="edit" id="results">
<apex:pageBlockTable value="{!contacts}" var="contact">
<apex:column headerValue="First Name">
<apex:outputField value="{!contact.firstName}"/>
</apex:column>
<apex:column headerValue="Last Name">
<apex:outputField value="{!contact.lastName}"/>
</apex:column>
<apex:column headerValue="Account">
<apex:outputField value="{!contact.account.name}"/>
</apex:column>
<apex:column headerValue="Technologies">
<apex:inputField value="{!contact.interested_technologies__c}" rendered="true"/>
</apex:column>
<apex:column headerValue="Technologies">
<apex:outputField value="{!contact.interested_technologies__c}" rendered="true">
</apex:outputField>
</apex:column>
<apex:column headerValue="Levels">
<apex:outputField value="{!contact.Level__c}" rendered="true">
</apex:outputField>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex class – ContactSearchController
public with sharing class ContactSearchController {
public List<Contact> contacts {get;set;}
public ContactSearchController()
{
contacts = [select firstname, lastname, account.name, interested_technologies__c, Level__c from contact limit 30];
}
public List<String> technologies
{
get
{
if (technologies == null) {
technologies = new List<String>();
Schema.DescribeFieldResult field = Contact.interested_technologies__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
technologies.add(f.getLabel());
}
return technologies;
}
set;
}
public List<String> levels{
get {
if (levels == null) {
levels = new List<String>();
Schema.DescribeFieldResult field = Contact.Level__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
levels.add(f.getLabel());
}
return levels;
}
set;
}
}
Problem and queries:
- Here, I can see Picklist values on VF Page for "Levels" and not for "Technologies".
- This is bit wierd behaviour as everything is the same except "Level__c" custom field was already created and "interested_technologies__c" custom field which I created.
- Am I missing or doing some mistakes here? Or Is there any accessibility issues?
- FYI, I can see both custom fields picklist values using inputfield.
- I am free user on Salesfroce and profile has been set as a System Administrator in my app.
Any recommendations would be highly appreciated.
The page looks like this for the moment
Thanks and regards,
Onkar.
Best Answer
Notice that you have two Technologies columns. The first one, which is of type
apex:inputField
works. I do not believeapex:outputField
supports the action you want because the fields are read-only. If you want the user to be able to select new values, you need to use the former. It looks like inputField works as desired without custom code, removing your need for a controller as well. If you need further assistance with how to manipulateapex:SelectList
components, I recommend asking a more directed question to that effect.From a best practice standpoint, you should have your column headers reference the $ObjectType global, for example: