Updating From Party In Email In Dynamic CRM

A tip on how to automate the update of From Party email in Dynamic CRM.


Recently while working on one business requirement where client had two business unit and they were using contacts specific to business unit which means they can have duplicate records for contact or account. While email coming into CRM they wanted to update email from partylist with correct contact from the correct business unit if wrong contact is auto populated by CRM. In this article we are going to provide details how we can update from partylist using pre operation plugin.


Check from party contact business unit, if it’s not correct update from with correct contact.


We can retrieve from party list from entity object and loop through it like following,
  1. if (emailEntity.Contains("from")) {  
  2.  EntityCollection from = emailEntity.GetAttributeValue < EntityCollection > ("from");  
  3.  //create sender list  
  4.  EntityCollection senderList = new EntityCollection();  
  5.  if (from != null) {  
  6.   from.Entities.ToList().ForEach(party => {  
  7.    Guid newContact = Guid.Empty;  
  9.    //get party list  
  10.    EntityReference partyId = party.GetAttributeValue < EntityReference > ("partyid");  
  11.    string contactName = party.GetAttributeValue<string>("addressused");  
  13.    //process partylist  
  15.   });  
  17.  }  
  19. }  

Our requirement is to check if from party list is contact type if yes, get and check if it is from correct BU if not get contact from correct BU, if not present create it and set it under from.

  1. if (partyId.LogicalName != null && partyId.LogicalName == "contact") {  
  2.  //retrieve current associated contact  
  3.  Entity currentAssociatedContact = emailLogic.GetContact(partyId.Id);  
  5.  //validate for correct contact   
  6.  if (!CheckContactForBU(currentAssociatedContact, ownerid)) {  
  7.   //create contact in correct BU  
  8.   newContact = CreateContact(contactName, ownerid);  
  10.   //add partylist to from  
  11.   Entity partyFrom = new Entity("activityparty");  
  12.   partyFrom["partyid"] = new EntityReference("contact", newContact);  
  13.   partyFrom["addressused"] = contactName;  
  15.   senderList.Entities.Add(partyFrom);  
  16.  }  
  17. }  

And finally we can assign sender list to from like following,

  1. if (senderList.Entities.Count > 0)    
  2. {    
  3.    emailEntity["from"] = senderList;    
  4. }    
We need to use this code on pre operation of email entity.This way we can change from email address before creation as after record created it can’t be changed.

Hope it will help someone !!