12

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?

14

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.

  • 1
    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 '16 at 13:07
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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