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.

Using the CRM Audit Log to Rollback Records

Argh! Someone has done something and something is ruined! Fortunately someone only changed records in CRM, and even better audit logging was enabled. You can now use the web service SDK to analyse changes that have occurred, you can then use this information to perform the opposite action and roll back changes.

Continue reading

CRM 2015 Building Action Links (or how a URL can change records in CRM)

I was looking for an easy way to allow a user to perform an action in CRM by opening a hyper link. The idea being the URL can be embedded in an email, SMS, or anything else, and simply opening the URL allows the user to perform an action in CRM (without having to actually use the full CRM interface).

I cobbled together this proof of concept which chains together a couple of components to create an “Action Link”.

aciton url process

Continue reading