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
Oldwen AdrianoOldwen Adriano 

SObject row was retrieved via SOQL without querying the requested field

Hello,

I am following a simple sample from https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

My code looks like this:
public with sharing class LeadController
{
	private final Lead ldObj;
	//public Lead ldObj{get;set;}
	
	public LeadController(ApexPages.StandardController controller)
	{
		//Fetching the Current Lead Record
		this.ldObj = (Lead)controller.getRecord();
	}
	
	public Component.Apex.SectionHeader getoutPanel() 
	{
		date dueDate = date.newInstance(2011, 7, 4);
		
		boolean overdue = date.today().daysBetween(dueDate) < 0;
		
		Component.Apex.SectionHeader sectionHeader = new Component.Apex.SectionHeader();
		
		if (overdue) 
		{
			sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!' + ldObj.FirstName;
			return sectionHeader;
		}
		else
		{
			sectionHeader.title = 'Form Submission';
			return sectionHeader;
		}
	}
}

When I run this, I get the following error:

Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Lead.FirstName

Can anyone please assist me with this?

Thank you in advance.....
 
William TranWilliam Tran
You are reference ldObj.FirstName (probably by accident) - but you never retrieve it anywhere, just remove it and it will like compile.

Change this:
sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!' + ldObj.FirstName;

To this:
sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!';

 
m.takam.taka
Please refer to the following
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_standardcontroller.htm#apex_ApexPages_StandardController_addFields

public LeadController(ApexPages.StandardController controller)
{
    // Add Reference Fields
    List<String> lists = new List<String>();
    lists.add('FirstName');
    controller.addFields(lists);
    //Fetching the Current Lead Record
    this.ldObj = (Lead)controller.getRecord();
}
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help u
public with sharing class LeadController
{
	private final Lead ldObj;
	//public Lead ldObj{get;set;}
	
	public LeadController(ApexPages.StandardController controller)
	{
		//Fetching the Current Lead Record
		this.ldObj = (Lead)controller.getRecord();
		if(ldObj.id != null)
		{
			ldObj = [select id,FirstName from lead where id =:ldObj.id ];
		}
	}
	
	public Component.Apex.SectionHeader getoutPanel() 
	{
		date dueDate = date.newInstance(2011, 7, 4);
		
		boolean overdue = date.today().daysBetween(dueDate) < 0;
		
		Component.Apex.SectionHeader sectionHeader = new Component.Apex.SectionHeader();
		
		if (overdue) 
		{
			sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!' + ldObj.FirstName;
			return sectionHeader;
		}
		else
		{
			sectionHeader.title = 'Form Submission';
			return sectionHeader;
		}
	}
}

Please let us know if above solution help u

Thanks,
Amit Chaudhary
Rakesh Kumar 335Rakesh Kumar 335
Error:  SObject row was retrieved via SOQL without querying the requested field:
Can you help me with proper syntax, please? Seems like the field needs to be in the query.

Current code:
 public static void AddOrderNumber(Map<Id, Order__c> oldMap,list<Order__c> issListIn,boolean isnew,boolean isODSvccall) {
       // List<Order__c> issListIn = [select id,Order_Number__c, from Order__c where id in :Ordermap.keyset()];
*The Query is in the commented line. Trying to save it, but unable to do so