function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Snita LalSnita Lal 

Lookup field in a Visualforce Page and Extension Controller

Hi,

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

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

public pagereference Save(){

erad__c cd = new erad__c();
cd.Account__c=cds.Account__c;
cd.Relationship_Type__c=cds.Relationship_Type__c;
cd.Date_Completed__c=cds.Date_Completed__c;
insert cd;
Pagereference pg = new Pagereference('/' + cd.id);
pg.setredirect(true);
return pg;


}
 
}

Thanks,

Snita
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