5

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;
}
2

2 Answers 2

1

If you DO know the object and field name and just wanna find components of the field, you can use:

public static String[] getComponentFields(String sobjectType, String addressFieldName) {
    EntityParticle[] particles = [
        SELECT QualifiedApiName
        FROM EntityParticle
        WHERE EntityDefinitionId = :sobjectType AND IsComponent = TRUE AND DeveloperName = :addressFieldName
    ];
    String[] ret = new String[]{};
    for(EntityParticle i: particles){
        ret.add(i.QualifiedApiName);
    }
    return ret;
}

Example

Executing:
======================================================================
System.debug(Objects.getComponentFields('Account', 'BillingAddress'));
======================================================================
Success.

14:42:55.120 (197861706)|USER_DEBUG|[1]|DEBUG|(BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry, BillingLatitude, BillingLongitude, BillingGeocodeAccuracy)
3
  • It looks like EntityParticle is a Tooling API object. That query still works from Apex?
    – Adrian Larson
    Oct 5, 2021 at 21:37
  • @AdrianLarson yes, but you need to provide either one of - EntityDefinitionId, FieldDefinitionId, DurableId - can't just filter by DeveloperName
    – dzh
    Oct 5, 2021 at 21:44
  • Executing query without fields above in Apex using Illuminated Cloud throws a cryptic error SoapFault INVALID_OPERATION: The requested operation is not yet supported by this sObject storage type...
    – dzh
    Oct 5, 2021 at 21:47
7

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;
    }
}
5
  • 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, 2018 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, 2018 at 19:51
  • Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
    – Adrian Larson
    Dec 28, 2018 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, 2018 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, 2018 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.