Trying to retrieve value of field from any given query on sobject. But when i have to traverse across multiple objects to fetch the value, I am not getting expected result.
Ex: to get value of owner.name in below query, I have to do
s.getSobject('Owner').get('Name');
I am able to build the above string properly but unable to fetch the value. Please look into my below code:
List<sObject> a = [select id, owner.name from case limit 1];
String[] so = new String[]{'id','owner.name'};
for(sObject s :a){
for(String str : so) {
string fieldvalue = str.replace(' ','');
if (str.contains('.')){
List<String> strList = new List<String>(str.split('\\.'));
String temp = 's.';
for(Integer i = 0; i < strList.size() -1; i++) {
temp += 'getSobject('+strList[i]+').';
}
temp += 'get(' + strList[strList.size()-1] + ')';
fieldValue = (string) temp; // getting correct string as expected but this not being evaluated to correct field value
} else {
fieldvalue = (string) s.get(fieldvalue); //This is working fine
}
system.debug(fieldvalue);
}
}