Plug-ins and Entity Reference fields. Things You Should Know.

Every now and then I run into an issue that I should have already known about.  Either that, or I knew about it and later forgot.  Regardless, let me tell you the story of the plug-in and the missing data.

 

Background

Let us say that I have a plug-in that is registered on the Create message for the Account entity. When a new Account is created, it will automatically populate a custom field with the name of the Primary Contact using code that looks like this:

var primaryContact = entity.GetAttributeValue<EntityReference>("primarycontactid");

entity["new_primarycontactname"] = 
    string.Format("{0}-{1:d}", primaryContact.Name, DateTime.Now);

This is pretty simple CRM SDK work that most of us do on a daily basis without much thought whatsoever.  All I am doing is taking the display name from the Entity Reference and using that value, along with the current date, to populate my custom field.

When the record is created with the CRM user interface, this code works perfectly. But, and there is always a but, what if we create the Account using an SDK call?

The answer is: It does not work so perfectly.

When you are creating an Entity record using the SDK the common method for creating an Entity Reference is to use code like this:

entity["primarycontactid"] = new EntityReference(Contact.EntityLogicalName, 
                                      Guid("9C06869F-50C6-E211-8D6B-00155D1E5005"));

This is the minimum requirement for populating an Entity Reference (Lookup) field using the SDK, and is generally how everyone does it.

 

The Problem

The problem is our plug-in.  Since the plug-in is registered on the Create message, it will fire for each Account being created, no matter if the record is coming from the Dynamics CRM web client, from a Scribe or SSIS job, or in this case, from a customer-facing web portal.

Since we did not specify the .Name property on our primarycontactid field, it will arrive inside of the plug-in as a Null value.

This means my plugin code will produce: "-4/8/2014” instead of “John Smith-4/8/2014.

This is not a bug in CRM, this is just the way it works.

 

The Work Around

So if we indeed need the .Name property on the Entity Reference, we need to either specify it in our code, like this:

var primaryContact = new EntityReference(
                              Contact.EntityLogicalName, 
                              new Guid("9C06869F-50C6-E211-8D6B-00155D1E5005"));
primaryContact.Name = "John Smith";

entity["primarycontactid"] = primaryContact;

OR, and this is not exactly a good thing, perform a read, from within the plug-in, of the Entity record referenced and pull back the primary field, like this:

var contact = organizationService.Retrieve(
                 patientReference.LogicalName, 
                 patientReference.Id, 
                 new ColumnSet(new[] { "fullname" }));

return patient["fullname"].ToString();

Anyway, this is something to keep in mind should you have an applications integrated with your Dynamics CRM system.

Leave a Reply 1 comment