0

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:

  1. 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.
  2. 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:

  1. Here, I can see Picklist values on VF Page for "Levels" and not for "Technologies".
  2. 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.
  3. Am I missing or doing some mistakes here? Or Is there any accessibility issues?
  4. FYI, I can see both custom fields picklist values using inputfield.
  5. 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 Here, as you can see in the screenshot that technologies fields are not appearing but Levels. My confusion here is why this is happening even though both custom fields has been accessed in the same way Thanks and regards, Onkar.

  • You may need write access on the field you created. – Adrian Larson Jan 16 '15 at 18:26
  • 1
    Your VF page is going to display up to 30 Contacts showing the values of each Contact's interested_technologies__c field. The page never references the two controller getters for technologies or levels which would normally be used in an apex:selectList component. Please clarify – cropredy Jan 16 '15 at 20:30
  • Yes, as crop1645 says, you are just using standard outputField instead of what is defined on your controller. My initial assumption was that you need to switch to inputField, but you said only one of the two picklists was not working. – Adrian Larson Jan 16 '15 at 23:26
  • @AdrianLarson I have checked field level accessibility and it shows editable rights. Is there any other accessibility/rights where I should focus on in this case? Yes, you got that right that one of picklist is not working. Please check screenshot that I have attached in edited question. Being beginner in Apex and VF any recommendations will be great. – OnkarDhane Jan 19 '15 at 10:57
  • @crop1645 OK, I understood what you want say. Thanks for that. But Please have a look in modified code snippet and respective screenshot. As a beginner I am curious/confused why it is working for Levels field and not with Technologies using apex:outputField ? Thanks in advance. – OnkarDhane Jan 19 '15 at 11:11
1

Notice that you have two Technologies columns. The first one, which is of type apex:inputField works. I do not believe apex: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 manipulate apex: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:

<apex:column headerValue="{!$ObjectType.Contact.Fields.Level__c.Label}">
1

It is unclear what you are trying to do with the contacts you are pulling from the page. However, if you just want to only list all the values of the two different picklists regardless of the contacts. You will want to do something like this:

<apex:page controller="ContactSearchController" sidebar="false">

  <apex:form >
<apex:pageBlock mode="edit" id="results">

<apex:pageBlockTable value="{!technologies}" var="Tech">
    <apex:column headervalue="Technologies">
        {!Tech}
    </apex:column>
</apex:pageBlockTable>

<apex:pageBlockTable value="{!contactlevels}" var="levels">
    <apex:column headervalue="Levels">
        {!levels}
    </apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Then your controller:

public with sharing class ContactSearchController {
public List<Contact> contacts {get;set;}
public List<String> contactlevels {get;set;}
public List<String> interestedtechnologies {get; set;}

public ContactSearchController() 
{
 //Not sure what you are trying to do with these contacts at this point.
 contacts = [select interested_technologies__c, Level__c from contact limit 30];
 interestedtechnologies = technologies;
 contactlevels = levels;
 }
  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;
  }
}

As you can see I added two List to be populated on the instanciation of the controller class. Hope this helps get you to where you want to go.

  • Thanks for the sample and it works. But Can't I show Technologies and Levels in one PageBlockTable in two columns? FYI, I want to show First Name, Last Name and Account and respective Technologies and Levels in one table. But in order to minimize the size of question I haven't put complete code for other fields. Thanks in advance. – OnkarDhane Jan 19 '15 at 10:39

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.