Have you tried sometimes retrieve the value of “AccountId” property from Contact entity? Or, have you tried retrieve the value of “OwningUser” from Contact entity? Which were the results? Nothing, weren’t they?. Don’t worry, your CRM works fine, you didn’t miss anything. The reason for this is found in “AggregateOf” property in entity attributes.
The point is the following:
Any attribute, whose property “AggregateOf” is NOT NULL, doesn’t return any value from CRM service requests
Which are these attributes? :
What does “AggregateOf” exactly mean? :
An attribute whose “AggregateOf” property in NOT NULL means that its value is part of other bigger aggregation expression in other attribute. For example, take accountid and parentcontactid. The value of their "AggreationOf” property is “parentcustomerid” for both. So, if we go to CRM database, in “Contact” view, we will see this:
ParentCustomerId = coalesce(ContactBase.AccountId, ContactBase.ParentContactId)
In other way:
ParentCustomerId =
case
when ContactBase.AccountId IS NOT NULL then ContactBase.AccountId
when ContactBase.ParentContactId IS NOT NULL then ContactBase.ParentContactId
else NULL
end
Why do I not retrieve any value from CRM Service?:
Because of these “special” attributes are part of other bigger aggregation expression, you should use that resulting attribute. In our previous example with Contact entity, you should retrieve “parentcustomerid” instead of “Accountid” or “ParentContactId”.
How can I know when I will have this “problem” and how can I resolve it?: You have two options:
- Check if attribute that you want to retrieve is in that previous list [I don’t like it, but It’s quick]
- Use CRM Metadata Service to check “AggreateOf” property [It’s less efficient, but it’s more flexible]. You can use this code to check it:
public string AggregateOfAttribute(string attributeName, string entityName)
{
RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = entityName;
request.LogicalName = attributeName;
request.RetrieveAsIfPublished = true;
MetadataService metadataService = new MetadataService();
//Set Metadata Service Properties (Url, Credentials, CrmAuthenticationTokenValue)
RetrieveAttributeResponse response = (RetrieveAttributeResponse)metadataService.Execute(request);
AttributeMetadata attributeMetadata = response.AttributeMetadata;
return attributeMetadata.AggregateOf;
}