开发者

Changing the From field for an email activity in a plug-in

开发者 https://www.devze.com 2023-03-26 23:52 出处:网络
When an email is sent to a queue and there is a contact associated with the \"From\" email in CRM, upon promoting an email to an email activity the system automatically fills in the \"From\" field wit

When an email is sent to a queue and there is a contact associated with the "From" email in CRM, upon promoting an email to an email activity the system automatically fills in the "From" field with the contact information. However, if a user with the same email exists in CRM, too, then the system always picks up the system user instead of the contact. I need to override this behaviour to ALWAYS pick up the contact if one with the email exists.

I created a post-operation plug-in (tried a pre-operation plug-in, too) for the event Create for email, trying to override the From field. The problem is, it does not work. When I debug the plug-in, it goes quietly past the assignment without any errors and then the same plug-in fires for the same email again. And again. And again.

When I try instead to create a new email and use the same ActivityList[] I was trying to use for the entity that triggered the event, it works. It seems that the problem is that CRM does not allow changing the From field from a plug-in, or am I doing something wrong? If it's a limitation enforced by CRM, is there a way around it?

My code is below:

var email = ((Entity)context.InputParameters["Target"]).ToEntity<Email>();

...

var oldFrom = ((EntityCollection)email.Attributes["from"]).Entities;
List<ActivityParty> newFrom = new List<ActivityParty>(); 
foreach (Entity party in oldFrom)
{
   EntityReference entRef = (EntityReference)party.Attributes["partyid"];
   if (entRef.LogicalName == SystemUser.EntityLogicalName)
      user = userLogic.Get(new Guid(entRef.Id.ToString()));
   if (user == null) return;
   string emailAddress = user.InternalEMailAddress;
   Contact contact开发者_开发百科 = contactLogic.LookupPASIndividual("", emailAddress);
   if (contact != null)
   { newFrom.Add(new ActivityParty() {PartyId = new EntityReference(Contact.EntityLogicalName, contact.ContactId.Value) });
   }
   else
      return;
}
email.From = newFrom;

Update: So I registered the plug-in on Pre-validation now and it's not triggered when an email activity is created by a router, it IS triggered when a user creates an email in CRM though...


The problem is that you aren't changing the email which is processed at all.

var email = ((Entity)context.InputParameters["Target"]).ToEntity<Email>();

This line converts the record which is currently processed to an object of type email. You modify the record which is not in scope of the operation. You have to modify the From of the target (either directly or write it back).

For the processing stages: take a look at the Event Execution Pipeline. Pre-Validation is to early for your task. I'am not quite sure when the address resolution is done, but I would try to do your conversion Pre-Create.


I ended up using a workaround: created an async Post-Event that associates the email activity with the contact if a contact with the same email exists, leaving the user associated with the email in the "From" field.

0

精彩评论

暂无评论...
验证码 换一张
取 消