I have created custom convert button on custom object which is to create account, contact & opportunity.Here i have attached the Apex code and visual force page code. After I click "convert" I am getting this error.

Error Message:

**SObject row was retrieved via SOQL without querying the requested field: CustomLead_c.CompanyName_c Error is in expression '{!convertlead}' in component in page leadconversion

An unexpected error has occurred. Your development organization has been notified.**

Apex code:

public class LeadConversion {
public PageReference RedirecttoLead()
String currentLead = '/' + leadObj.Id;
PageReference pageRef = new PageReference(currentLead);
return pageRef;
private CustomLead__c leadObj;

// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public LeadConversion(ApexPages.StandardController stdController)
leadObj = (CustomLead__c)stdController.getRecord();

public void convertLead(){

Account acc = new Account();
acc.Companynamelead__c = leadObj.CompanyName__c;
//acc.BillingAddress = leadObj.Address;

insert acc;
Catch (Exception ex1)

Contact cc = new Contact();
cc.Lastname_lead__c = leadObj.LastName_c__c;
//cc.BillingAddress = leadObj.Address;

insert cc;
Catch (Exception ex2)


Opportunity opp = new Opportunity();
opp.opp_namelead__c = leadObj.opp_name__c;
//opp.BillingAddress = leadObj.Address;

insert opp;
Catch (Exception ex3)




Visual force code:

<apex:page standardController="CustomLead__c"  cache="true" action="{!convertlead}" extensions="LeadConversion" >
<apex:messages style="color:red; font-weight:bold; text-align:center;"/>
<apex:form >
<apex:commandLink value="Redirect to Lead" style="color:blue; font-weight:bold;" action="{!RedirecttoLead}"/>
  • 2
    I've answered the version of this that you posted on the developerforce boards. Commented Apr 3, 2013 at 12:05

1 Answer 1


Generally what this means is that your controller is trying to reference a field that has not been populated. By default, Visualforce controllers will retrieve values for all fields referenced in the Visualforce page and no others. You can solve this in one of two ways:

  1. Explicitly retrieve the field in the controller's method
  2. Add the field to your VF page with an output field that has rendered="false":

    <apex:outputText value="{!CompanyName_c}" rendered="false" />

My preference is to use the outputText field for the following reasons:

  1. It saves a SOQL call
  2. I think it aids in maintenance. In the future other admins will see that the field is necessary to the VF page's functionality, even if it is not visible.


As Andrew mentioned in the comments, there is a third option: use the addFields() method on the StandardController. This might be the cleanest option since it does not add clutter to your VF page.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .