+ Start a Discussion
Ashwani PradhanAshwani Pradhan 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field

I am running below SOQL in Developer Console and its working fine -
SELECT MSRE_Contact__r.name, MSRE_Contact__r.MSRE_Company__c, MSRE_Contact__r.MSRE_Contact_Title__c, MSRE_Investor__r.name, MSRE_Investor__r.Parent.Name, MSRE_Investment__c, MSRE_Investment__r.Product2.name, MSRE_Contact_Relationship__c, MSRE_Distribution__c, MSRE_Quarterly__c, MSRE_Legal__c, MSRE_Tax__c, Webcast__c, MSRE_Annual_Meeting__c, MSRE_Adv_Committee__c, CreatedDate, LastModifiedDate, lastModifiedBy.Name, IsDeleted FROM MSRE_Correspondence__c

Now same query if I am using in Apex calss below error coming -
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field. Asset.Name

Here Product2 is lookup in Investment(Asset). Why its working in Developer Console and not in Apex? Any help would be appreciated.
Amit Chaudhary 8Amit Chaudhary 8
Please add "Asset.Name" field in query

if "Asset.Name" field is on MSRE_Correspondence__c object
SELECT MSRE_Contact__r.name, MSRE_Contact__r.MSRE_Company__c, MSRE_Contact__r.MSRE_Contact_Title__c, MSRE_Investor__r.name, MSRE_Investor__r.Parent.Name, MSRE_Investment__c, MSRE_Investment__r.Product2.name, MSRE_Contact_Relationship__c, MSRE_Distribution__c, MSRE_Quarterly__c, MSRE_Legal__c, MSRE_Tax__c, Webcast__c, MSRE_Annual_Meeting__c, MSRE_Adv_Committee__c, CreatedDate, LastModifiedDate, lastModifiedBy.Name, IsDeleted ,Asset.Name  FROM MSRE_Correspondence__c
Please post your full code and line number where you are getting error.

NOTE:- you need to add field in above query.
 
Krishna SambarajuKrishna Sambaraju
Hi Ashwani,

You might have referred "Asset.Name" in the visualforce page using the controller or in the controller some where, which you didn't query. If "Asset" is a lookup on MSRE_Investment__c object then, you should add MSRE_Investment__r.Asset.Name in your query before using it.

Hope this helps.

Kind Regards,
Krishna.
Ashwani PradhanAshwani Pradhan
Thanks guys! I did blunder, was using a field Asset.Name in Apex but not included in SQL.
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
Krishna SambarajuKrishna Sambaraju
Hi Rakesh,

You might be using a field some where in the Apex code, which you haven't included in the SOQL query. Review the code properly and add the missing field in the SOQL query. Post the complete code if you need further help on this.

Regards,
Krishna.