Get components of a compound field?
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
add a comment |
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
add a comment |
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
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
apex custom-metadata fielddefinition entityparticle
asked 3 hours ago
Adrian Larson♦
104k19112235
104k19112235
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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♦
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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♦
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
add a comment |
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♦
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
add a comment |
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;
}
}
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;
}
}
edited 2 hours ago
answered 2 hours ago
David Reed
30k61746
30k61746
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♦
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
add a comment |
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♦
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
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♦
1 hour ago
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♦
1 hour ago
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
1 hour ago
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
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
Well Avrom said Winter 19 was the target for release, so maybe it will come out soon.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
One thing you could do is check if the first word of the field name matches before getting any describes.
– Adrian Larson♦
1 hour ago
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
55 mins ago
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
55 mins ago
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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