+ Start a Discussion
Snita LalSnita Lal 

Lookup field in a Visualforce Page and Extension Controller


We've built a controller that we use for our custom object that just allows us to show the content in a visualforce page, slightly different to a standard Salesforce layout. 

Everything seems to work perfectly until we start to introduce lookup fields to the account because the user will always create a new custom object Erad__c from the account the value will always be populated, however when we reference this field just in the same way as a text or number field the value isn't stored. We are relatively new to this sort of thing so any advice that could be offered would be appreciated.

I'm guessing that we need to store the value in the controller correctly and refernce back to the visualforce page but we're unsure on how to exactly go about it.

Visualforce page
<apex:page standardController="Erad__c" extensions="insertnewrecordController">

Hello {!$User.FirstName}!    
   <p>You are viewing the Erad model. Please enter the Relationship grade below based on the image and the date your assesment was made.</p>

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField id="Account" value="{!Erad__c.Account__c}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel >Relationship Type</apex:outputLabel>
<apex:inputField id="RelationshipType" value="{!Erad__c.Relationship_Type__c}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel >Date Completed</apex:outputLabel>
<apex:inputField id="DateCompleted" value="{!Erad__c.Date_Completed__c}"/>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
<apex:image url="{!$Resource.Erad}" width="500" height="500"/>

public class insertnewrecordController{
public erad__c cds{get; set;}
public erad__c setcds(erad__c op){
return cds;
public insertnewrecordController(ApexPages.StandardController controller) {
cds=new erad__c();

public pagereference Save(){

erad__c cd = new erad__c();
insert cd;
Pagereference pg = new Pagereference('/' + cd.id);
return pg;



Best Answer chosen by Snita Lal
Snita LalSnita Lal
Thanks Matthew, we solved this by using a URL hack.

All Answers

Matthew CokeMatthew Coke
Use the Id field of the object, not the object itself. For example, if using account, use AccountId instead of Account__c.

Additionally, you can use relationship queries and the "." accessor to pull in information from the account
Snita LalSnita Lal
Thanks Matthew, we solved this by using a URL hack.
This was selected as the best answer