Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have written this controller extension:

public with sharing class PopupController {
    ApexPages.Standardcontroller cont;
    public Request_Change_Reseller__c req{get;set;}

    public PopupController(ApexPages.StandardController cont){
    this.cont = cont;
    req=new Request_Change_Reseller__c();
    req.PartnerUserId__c = ((Lead)cont.getRecord()).Assigned_to_Partner__c;

    }

    public PageReference save(){

    return null;
    }

}

My visualforce page is:

<apex:page standardController="Lead" extensions="PopupController" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
  <apex:outputField label="Lead" value="{!Lead.Name}"/>
  <apex:inputField label="Assign To Partner" value="{! req.PartnerUserId__c}" />
<apex:commandButton value="Submit" action="{!save}" />
</apex:pageBlockSection>
</apex:pageBlock>
  </apex:form>
</apex:page>

I created a custom button in the lead layout. When I click on it a new window opens and I get this error:

SObject row was retrieved via SOQL without querying the requested field: Lead.Assigned_to_Partner__c .

Can I not access all fields in a controller extension? Will I have to make a soql query?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

A standardcontroller's record will only contain the fields referenced in the visualforce page you're using it on. The standard controller addFields(List) method (docs) allows you to extend this to the fields you need in your apex code.

cont.addFields('Assigned_to_Partner__c');

Set this as the 2nd line in your constructor.

share|improve this answer
add comment

A field has to be on the page to be included - so as well as using addfields, you can also just add the field in an output panel with rendered set to false - it should then be included in the SOQL

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.