2

This question is related to help I received on a separate question: Get fieldnames from List.

I have since implemented this code:

for (DopplerListSubscriber__c susc : sobjList) {
    Map<String,Object> fieldsPopulated = susc.getPopulatedFieldsAsMap();
    System.debug(fieldsPopulated);
}

I'm getting the following debug log:

15:59:58:141 USER_DEBUG [35]|DEBUG|{Contact__c=00346000002O4YlAAK, Contact__r=Contact:{Id=00346000002O4YlAAK, Name=Jack Rogers, Email=jrogers@burlington.com}, DopplerList__c=a0046000001OvCGAA0, Id=a0246000001nTMdAAM}

The problem is, how do I access to Name and Email fields from the Contact__r key? That is:

Contact__r=Contact:{Id=00346000002O4YlAAK, Name=Jack Rogers, Email=jrogers@burlington.com}
1
  • the __r is a notification it is a Lookup reference. This means that you are getting a reference to the object identified by the Id in the field. Essentially, you can use it like an SObject.
    – zgc7009
    Mar 23, 2017 at 20:05

2 Answers 2

6

Contact__r looks like it's an object. You can get the object by doing this:

Contact cnt = (Contact)fieldsPopulated.get('Contact__r');
system.debug('Email: ' + cnt.Email);
system.debug('Name : ' + cnt.Name);

==UPDATE (Per Comments)==

If you don't know the type of object that will be returned you can do this

Object obj = fieldsPopulated.get('Contact__r');
system.debug('Email: ' + obj.get('Email'));
system.debug('Name : ' + obj.get('Name'));
6
  • I cannot cast to Contact object since I have dynamic objects which I need to get before I get the field value.
    – BoDiE2003
    Mar 23, 2017 at 20:19
  • You have to cast to a specific object, because that is the type that is returned by the method. I've added some code Mar 23, 2017 at 21:15
  • You don't have to cast the object type per se. You could store it as an SObject and use dynamic get methods (you're not even taking advantage of static getters anyway).
    – Adrian Larson
    Mar 23, 2017 at 21:34
  • That's what my updated code shows.... not sure if you saw the update Mar 23, 2017 at 21:45
  • The last code is much more usefull for the case Im implementing it in, but it says the next Error:(38, 30) Method does not exist or incorrect signature: [Object].get(String)
    – BoDiE2003
    Mar 27, 2017 at 13:33
1

I will prefer to write this logic to find out the field types.

getFieldType() method will take parameters like SObject and fieldName and will return the field type.

for (DopplerListSubscriber__c susc : sobjList) {
    Map<String,Object> fieldsPopulated = susc.getPopulatedFieldsAsMap();

    //since fieldsPopulated is returning
    /*
    {Contact__c=00346000002O4YlAAK, Contact__r=Contact:{Id=00346000002O4YlAAK, 
    Name=Jack Rogers, Email=jrogers@burlington.com}, 
    DopplerList__c=a0046000001OvCGAA0, Id=a0246000001nTMdAAM}
    */

    Set<String> fields = fieldsPopulated.keySet();
    for(String str:fields)
    {
        Schema.DisplayType dt = getFieldType(Contact, str);

        if(fd.getType() == Schema.DisplayType.String)
        {                       
            //handle String type and retrieve values from API Name
            System.debug((String) fieldsPopulated.get(str));
        }
    }
}

public Schema.DisplayType getFieldType(SObject obj, fieldName)
{
    Map<string, schema.sobjectfield> fieldMap = obj.getDescribe().fields.getMap();
    if (fieldMap != null)
    {
        // Loop through all fields.
        for (Schema.SObjectField f : fieldMap.values())
        {
            // Describe each field.
            Schema.DescribeFieldResult fd = f.getDescribe();
            if(fd.getName().equals(fieldName)
            {
                return fd.getType();
            }                           
        }
    }
}
1
  • it says cannot resolve symbol 'fieldName' on public Schema.DisplayType getFieldType(SObject obj, fieldName)
    – BoDiE2003
    Mar 27, 2017 at 13:25

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.