1

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);
    }
}
  • 1
    Not sure why you're manipulating the fields you're given... take a look at FieldReference, it could save you a lot of work. – Adrian Larson Feb 20 '16 at 17:30
2

If I were you, I would just copy the FieldReference class into your org and use it as follows:

Opportunity someRecord = [SELECT Account.Owner.Name FROM Opportunity LIMIT 1];
Object owner = FieldReference.build('Account.Owner.Name').getFrom(someRecord);

However, if you prefer to roll your own method, it should look something like:

public with sharing class CrossObject
{
    public static Object get(SObject record, String field)
    {
        if (record == null) return null;
        if (!field.contains('.')) return record.get(field);
        return get(
            record.getSObject(field.substringBefore('.')),
            field.substringAfter('.')
        );
    }
}

The first example would still work here, though you would have to change your signature slightly.

Object owner = CrossObject.get(someRecord, 'Account.Owner.Name');

Bonus in using FieldReference is that the unit tests are already written for you.

2

Here the problem is that temp is a String and you try to assign one String into other. Remember s.getSobject('Owner').get('name') returns object and to convert into String user try to typecast into String that you did. So it is not possible to get the right answer until you use it as a String. Hope it clears you. You can use the below code

List<sObject> a = [select owner.name, id 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 first = '';

            String second = '';

            for(Integer i = 0; i < strList.size() -1; i++) {

                first = strList[i];

            } 
            second = strList[strList.size()-1];
            //fieldValue = (string) obj;    // getting correct string as expected but this not being evaluated to correct field value        
            fieldValue = (String)s.getSObject(first).get(second);
        } else {
            fieldvalue = (string) s.get(fieldvalue); //This is working fine
        }
        system.debug(fieldvalue);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.