4

I am currently running into this limitation of Custom Metadata:

Custom Metadata Relationships and Compound Fields

Since it is still not possible to relate a record to a specific EntityParticle (e.g. BillingStreet) rather than the entire FieldDefinition (e.g. BillingAddress), I would like to know if it is possible to get the component fields which make up a compound field via Apex, with no callouts.

Desired state:

public static List<SObjectField> getEntityParticles(SObjectField field)
{
    List<SObjectField> particles = new List<SObjectField>();
    if (fieldIsCompound)
    {
        // get the fields which make up the compound field specified
        // without using any callouts
    }
    return particles;
}
5

Yes, by using JSON serialization or by referencing an undocumented property compoundFieldName on Schema.DescribeFieldResult.

The API version of a field describe result includes this key. If non-null, the present field is a component of a compound field, whose API name is populated in that key.

This field is not (documented to be) available on Schema.DescribeFieldResult, but if you serialize the object, the data is present. Additionally, it can be referenced in Apex, even through it's undocumented:

Contact.OtherStreet.getDescribe().compoundFieldName

or

Contact.OtherStreet.getDescribe().getCompoundFieldName()

Hence, an approach like this is possible:

public class CompoundFieldUtil {
    public static List<SObjectField> getEntityParticles(SObjectType objectType, SObjectField field) {
        Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
        List<SObjectField> components = new List<SObjectField>();
        String thisFieldName = field.getDescribe().getName();

        for (String s : fieldMap.keySet()) {
            if (fieldMap.get(s).getDescribe().compoundFieldName == thisFieldName) {
                components.add(fieldMap.get(s));
            }
        }

        return components;
    }
}

Then,

System.debug(CompoundFieldUtil.getEntityParticles(Contact.sObjectType, Contact.OtherAddress));

yields

14:15:14:523 USER_DEBUG [1]|DEBUG|(OtherStreet, OtherCity, OtherState, OtherPostalCode, OtherCountry, OtherStateCode, OtherCountryCode, OtherLatitude, OtherLongitude, OtherGeocodeAccuracy)

The JSON serialization form also works, but is approximately five times slower:

public class CompoundFieldUtil {
    public static List<SObjectField> getEntityParticles(SObjectType objectType, SObjectField field) {
        Map<String, SObjectField> fieldMap = objectType.getDescribe().fields.getMap();
        List<SObjectField> components = new List<SObjectField>();
        String thisFieldName = field.getDescribe().getName();

        for (String s : fieldMap.keySet()) {
            Map<String, Object> describeData = (Map<String, Object>)JSON.deserializeUntyped(
                JSON.serialize(fieldMap.get(s).getDescribe())
            );

            if (describeData.containsKey('compoundFieldName')
                && (String)describeData.get('compoundFieldName') == thisFieldName) {
                components.add(fieldMap.get(s));
            }
        }

        return components;
    }
}
  • Oh man that's slow. I was hoping it might be possible to figure out without iterating every single field on the object. – Adrian Larson Dec 28 '18 at 19:49
  • Yeah, it's not great. The JSON version eats about half a second, the non-JSON version about a tenth. I don't know of a way to do it without iteration but I'd love to be wrong (I need this for a project too). – David Reed Dec 28 '18 at 19:51
  • Well Avrom said Winter 19 was the target for release, so maybe it will come out soon. – Adrian Larson Dec 28 '18 at 19:57
  • One thing you could do is check if the first word of the field name matches before getting any describes. – Adrian Larson Dec 28 '18 at 20:39
  • Yeah, I guess another way to approach it would be mostly heuristic. We know the component name pattern for any field of type Address or custom Geolocation. What does that leave out besides compound Name fields? – David Reed Dec 28 '18 at 20:48

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.