Get components of a compound field?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







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









share|improve this question





























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









    share|improve this question

























      5












      5








      5


      2






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









      share|improve this question














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






      apex custom-metadata fielddefinition entityparticle






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 18:33









      Adrian LarsonAdrian Larson

      110k19121259




      110k19121259






















          1 Answer
          1






          active

          oldest

          votes


















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





          share|improve this answer


























          • 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








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244947%2fget-components-of-a-compound-field%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









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





          share|improve this answer


























          • 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
















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





          share|improve this answer


























          • 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














          5












          5








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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 19:32

























          answered Dec 28 '18 at 19:16









          David ReedDavid Reed

          39.5k82357




          39.5k82357













          • 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



















          • 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

















          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





          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





          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





          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





          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





          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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244947%2fget-components-of-a-compound-field%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bundesstraße 106

          Verónica Boquete

          Ida-Boy-Ed-Garten