Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}" />

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
up vote 8 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(new List<String>{'Assigned_to_Partner__c'});

Set this as the 2nd line in your constructor.

share|improve this answer
Dunno if it was the case when you wrote this, but worth noting this method cannot be called from a test. – Adrian Larson Jun 21 at 13:07

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

Your Answer


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.