I am getting

"Line: 24, Column: 1 System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Contact__r"

Here is my Code can you please help me out to find relational object's field value

String sTemp = 'Contact__r.CreatedBy.profileid';
public String getAllQuery {get;set;}
List<Schema.SObjectField> tempFields;
public Id oid = '00128000003P6nL';
public SObject resultObject {get;set;}
public List<Schema.DescribeFieldResult> fields {get;set;}
public static final String ERROR_ID_MISSING = 'There was no id passed in the parameters. Id is required.';
public static final String QUERY_SELECT = 'select ';
public static final String QUERY_FROM = ' from ';
public static final String QUERY_WHERE = ' where Id = \'';
fields = new List<Schema.DescribeFieldResult>();
Schema.DescribeSObjectResult objDescribe = oid.getSObjectType().getDescribe();
  Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
  tempFields = fieldMap.values();
  for(Schema.SObjectField sof : tempFields){
  getAllQuery = buildQueryAllString(fields,objDescribe,oid);
  resultObject = Database.query(getAllQuery);
    String[] arrTest = sTemp.split('\\.');
    String sRelatedValue = '';
    sObject tempObject ;
    tempObject = (SObject)resultObject.getSObject(arrTest[0]);
public String buildQueryAllString(List<Schema.DescribeFieldResult> queryFields,DescribeSObjectResult obj, String theId){
  String query = QUERY_SELECT;
  for(Schema.DescribeFieldResult dfr : queryFields)
      query = query + dfr.getName() + ',';
  query = query.subString(0,query.length() - 1);
  query = query + QUERY_FROM;
  query = query + obj.getName();
  query = query + QUERY_WHERE;
  query = query + theId + '\'';
  system.debug('Build Query == ' + query);
  return query;
  • Account.Contact__r yo are trying to access this without querying. Please check these lines if(sTemp.contains('.')){ String[] arrTest = sTemp.split('\\.'); String sRelatedValue = ''; sObject tempObject ; tempObject = (SObject)resultObject.getSObject(arrTest[0]); } Sep 10, 2015 at 5:56

2 Answers 2


It looks like you have built a function to query all of the fields on a particular object - Account in this case, base on the value stored in oid.

After performing that query, you are trying to retrieve the value of the field named: Contact__r. This is where the error occurs. There is no field named Contact__r (or profileid for that matter) on the Account object.

Even if you left the sTemp string intact instead of splitting it, you would still run into this same type of error because buildQueryAllString only queries fields on one object, and doesn't query fields on related objects.

In your sTemp string, Contact__r.CreatedBy.profileid, you are traversing two relationships. You go from the Account to a Contact to a User; It is the User object where the profileid is actually located.

  • There is a custom field in Account, Contact Contact__c Lookup(Contact) Than why it's throwing exception for Contact Relation Sep 10, 2015 at 6:11
  • @kumargaurav : So, when you set a value for that look-up relationship, you will have an id value for the field Contact__c on Account. Your query that gets generated in buildQueryAllString will only get that id value. It won't get the value of any of the fields on the Contact object.
    – martin
    Sep 10, 2015 at 6:13
  • Ok i missed that, now i think i should query again in base of contactid like i have query for Account in base of Id Sep 10, 2015 at 6:17
  • @kumargaurav : If you just want to get the id of the Contact, you can do a query for the Contact__c field. If you want to get the Id of the owner of that Contact, you can query for Contact__r.OwnerId. Keep in mind though that the __r notation can't be used alone.
    – martin
    Sep 10, 2015 at 6:22
  • @kumargaurav With what you already have, it would probably be easiest to just query again once you get the id since you already have the function to get all fields on an object just from an id. Keep in mind though that this probably not the best option performance-wise. In general, you want to use as few queries as possible.
    – martin
    Sep 10, 2015 at 6:57

As @martin has pointed out you are missing the child relations in your query and trying to access them in your results set.
Use getChildRelationShips() on your objDescribe instance and collect the required child fields as well to pass into the query. ChildRelationShip Class also may helpful for you.

  • The relationship is the other way around. The field is on Account, looking up a Contact.
    – martin
    Sep 10, 2015 at 6:53

You must log in to answer this question.

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