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){
fields.add(sof.getDescribe());
}
getAllQuery = buildQueryAllString(fields,objDescribe,oid);
resultObject = Database.query(getAllQuery);
if(sTemp.contains('.')){
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;
}