I’m a relationship, I’m not a many to many, I’m not a lookup, what am I?

CRM has a two types of relationships:

  • One to many: a lookup on one entity, a grid on the other.
  • Many to many: a grid on both entities.

You are looking at a lookup. 90% of the lookups you see are simple, you can select one record of one entity type.

However, there are special system field lookups, namely the activity party. These muddy the waters a little. Some activity party lookups allow multiple record selections, some link to entities of multiple types. Which means you can have an activity party lookup with multiple records of multiple entity types. I suppose you could call this a “one to many (records) of many (entities)”.

For example on an email; the To field can be populated with multiple account and contact records. Whilst the From field can only have one record, but that can be a system user or queue. The To and From are both examples of activity party lookups.

To tell the difference programmatically you can use a RetrieveAttributeRequest. AttributeType shows if its an activity party, whilst Targets allows you see what record types are allowed in the lookup.

The following code shows how to do this for a couple of different fields.


RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = "email",
LogicalName = "to",
RetrieveAsIfPublished = true
};

RetrieveAttributeResponse result = Service.Execute(attributeRequest) as RetrieveAttributeResponse;

Trace.WriteLine("Email - To");
Trace.WriteLine("AttributeMetadata.AttributeType: " + result.AttributeMetadata.AttributeType);
Trace.WriteLine("LookupAttributeMetadata.Targets: " + ((LookupAttributeMetadata)result.AttributeMetadata).Targets.CollectionToString(", "));

attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = "email",
LogicalName = "from",
RetrieveAsIfPublished = true
};

result = Service.Execute(attributeRequest) as RetrieveAttributeResponse;

Trace.WriteLine("Email - From");
Trace.WriteLine("AttributeMetadata.AttributeType: " + result.AttributeMetadata.AttributeType);
Trace.WriteLine("LookupAttributeMetadata.Targets: " + ((LookupAttributeMetadata)result.AttributeMetadata).Targets.CollectionToString(", "));

attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = "account",
LogicalName = "parentaccountid",
RetrieveAsIfPublished = true
};

result = Service.Execute(attributeRequest) as RetrieveAttributeResponse;

Which outputs:

Email – To
AttributeMetadata.AttributeType: PartyList
LookupAttributeMetadata.Targets: account, contact, lead, queue, systemuser

Email – From
AttributeMetadata.AttributeType: PartyList
LookupAttributeMetadata.Targets: queue, systemuser

Account – Parent Account Id
AttributeMetadata.AttributeType: Lookup
LookupAttributeMetadata.Targets: account

Originally posted on StackExchange here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s