I'm trying to set an entity reference from within a plugin. It's on the associate of two entities - stock and application form. When a stock item is associated with an application form, I get the name of the applicant (which is another entity - Contact) from a look up on the application form, and populate a look up on the stock item with that contact.
Here is my code:
private const string ASSOCIATE = "Associate"; private const string DISASSOCIATE = "Disassociate"; public void Execute(IServiceProvider serviceProvider) { try { PluginSetup(serviceProvider); if (context.MessageName == ASSOCIATE || context.MessageName == DISASSOCIATE) { // Get the Relationship Key from context if (context.InputParameters.Contains("Relationship")) { string relationshipName = context.InputParameters["Relationship"].ToString(); // Check the Relationship Name with intended one if (relationshipName == "applicationForm_stock") { if (context.MessageName == ASSOCIATE) { SaveAssociatedStock(ASSOCIATE); } else if (context.MessageName == DISASSOCIATE) { SaveAssociatedStock(DISASSOCIATE); } } else { return; } } } } catch (Exception ex) { throw new InvalidPluginExecutionException("An error occurred"); } } private void SaveAssociatedStock(string messageType) { try { EntityReference applicationFormEntity = null; EntityReferenceCollection stockEntities = null; EntityReference contactEntityReference = null; Entity stockItem = new Entity(); ColumnSet nameCol = new ColumnSet(); nameCol.AddColumn("name"); ColumnSet contactNameCol = new ColumnSet(); contactNameCol.AddColumn("contactname"); // Get Application Form entity reference from Target Key from context if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) { applicationFormEntity = (EntityReference)context.InputParameters["Target"]; Trace("applicationFormEntity.Id: " + applicationFormEntity.Id); Entity test = service.Retrieve("applicationForm", applicationFormEntity.Id, contactNameCol); // Get the contact name from the contact lookup on the application form contactEntityReference = (EntityReference)test.Attributes["contactname"]; } // Get Stock reference from RelatedEntities Key from context if (context.InputParameters.Contains("RelatedEntities") && context.InputParameters["RelatedEntities"] is EntityReferenceCollection) { stockEntities = context.InputParameters["RelatedEntities"] as EntityReferenceCollection; for (int i = 0; i < stockEntities.Count; i++) { // Get name of current Stock item stockItem = service.Retrieve("stock", stockEntities[i].Id, nameCol); if (messageType == ASSOCIATE) { // Add the contact to the current Stock item record Entity updateStockItem = new Entity("stock"); updateStockItem.Attributes.Add(new KeyValuePair<string, object>("stockid", stockEntities[i].Id)); updateStockItem.Attributes.Add(new KeyValuePair<string, object>("owner", contactEntityReference.Id)); service.Update(updateStockItem); } else if (messageType == DISASSOCIATE) { // Remove the contact from the current Stock item record Entity updateStockItem = new Entity("stock"); updateStockItem.Attributes.Add(new KeyValuePair<string, object>("stockid", stockEntities[i].Id)); updateStockItem.Attributes.Add(new KeyValuePair<string, object>("owner", null)); service.Update(updateStockItem); } } }
However, I'm getting this error:
<Message>An error occurred in the AssociateEntities.AssociateEntities.Execute plug-in.An error occurred in the AssociateEntities.AssociateEntities.SaveAssociatedStock plug-in.System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #863F0E66</Message>
All my IDs have valid values in them, as I've already put in trace logging, and can see that they are correct.
Thanks.