2

I have a custom visualforce page and controller and am attempting to populate a lookup field on that page. Often the lookup will contain a value already, and I would like for the value to . According to the documentation here, I should just be able to use <apex:inputField value="{!contact.custom_reference__r.Name}"/>. However, I seem to be missing something. With the following controller and page, the debug prints all show up correctly but the values on the visualforce page show up empty. Why would this be?

Here is the custom controller; all of the debug prints output the expected values:

public with sharing class StudentPortal_ProfileController 
{
    public Contact profileInfo {get;set;}

    public StudentPortal_ProfileController() 
    {
        Id userId = UserInfo.getUserId();
        List<User> UserList = [select U.ContactId from User U where Id =: userId];
        Id contactId = UserList.get(0).ContactId;
        Contact profileInfo = [SELECT Id, FirstName, Profile_Home_Institution__c, Profile_Home_Institution__r.Id, Profile_Home_Institution__r.Name FROM Contact WHERE Id =: contactId LIMIT 1];


        system.debug(profileInfo.Profile_Home_Institution__r.Id);
        system.debug(profileInfo.Profile_Home_Institution__c);
    }

}

Here is the VF page, which doesn't render anything for the <apex:inputField>:

<apex:page docType="html-5.0" controller="StudentPortal_ProfileController" extensions="AutoCompleteController" action="{!forwardToCustomAuthPage}" showHeader="false" sidebar="false" standardStylesheets="false">
<apex:form styleclass="form-horizontal col-lg-12 col-md-12 col-sm-12 col-xs-12">
    <fieldset>
        <apex:inputField value="{!profileInfo.Profile_Home_Institution__c}"/>
    </fieldset>
</apex:form>
</apex:page>

Even adding just <h1>{!profileInfo.Profile_Home_Institution__r.Name}</h1> to the page renders nothing. Why would this be?

4
  • I think fields on related object are not writable. Does outputText work?
    – dzh
    Commented Jun 12, 2015 at 3:29
  • @cameron_Ames: Can you remove the with sharing and try? I feel there could be an OWD setting that is messing the view? Also what is the OWD setting for the related object?
    – Sam
    Commented Jun 12, 2015 at 13:45
  • @dzh outputText does not work. I tried it with the Profile_Home_Institution__c field and the Profile_Home_Institution__r.Name field. Commented Jun 12, 2015 at 13:49
  • @Mugambo The OWD for contact was 'Controlled by Parent.' I changed it to Public Read/Write and there was no change to the UI. The custom obect was already at Public Read/Write. Commented Jun 12, 2015 at 13:55

2 Answers 2

1

I found the answer to my own question. @Mugambo put me on the right track with checking Organization-Wide Defaults. I was using a standard profile, which did not have CRUD access to my custom object (the child end of the lookup relationship.) I cloned the standard profile and added the CRUD permissions to the new profile. Everything works now.

0

The form you quote in the question:

<apex:inputField value="{!profileInfo.Profile_Home_Institution__c}"/>

should present an editable lookup field that is pre-populated. The platform recognised the field as a foreign key ID and presents the name of the referenced object but updates the ID value.

Using contact.custom_reference__r.Name would allow you to edit that name and using contact.custom_reference__r.Id will just present the ID value without the ability to edit it.

Is the field Profile_Home_Institution__c visible and not read-only in the profile you are using? Obviously another reason for the field to be presented empty is if the particular object you are looking at doesn't have the lookup relationship set.

3
  • According to their profile, they should have access, as the field is visible and NOT read-only. The data is populating correctly, when I check those debug outputs the ID and name of the lookup object is there. It is only on the VisualForce page that it renders nothing. Commented Jun 12, 2015 at 11:59
  • @Cameron.Ames Strange. My only other thought is to eliminate the relatively unusual page settings you are using i.e. try <apex:page controller="StudentPortal_ProfileController"> to see if that makes any difference.
    – Keith C
    Commented Jun 12, 2015 at 12:06
  • Unfortunately that did not work. I also tried using the standard controller to see if it was something with my custom controller. Unfortunately I receive the same results. Commented Jun 12, 2015 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .