![]() |
#1 |
Участник
|
mscrmblog: CRM 2011 – Set the Email regarding field to the contact in the recipient address
Источник: http://mscrmblog.net/2013/11/13/crm-...pient-address/
============== Scenario: An email gets sent out and copies in the Queue Email Address. The Email router picks up the email and sticks it in the queue. Client wanted to automatically track the the email against the contact in the recipient address automatically (note: all contacts getting emails sent to already exist as contacts in the system). There was no way to do this using the workflow engine since the Recipient field and the Regarding field are two different types (partylist vs entityreference). I created a plug-in to do the job. Register the plugin on pre-operation and this should do the job. using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WC.CRM { public class WCEmailPlugin : IPlugin { public void Execute(IServiceProvider serviceProvider) { var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.Depth > 1) return; var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); var service = serviceFactory.CreateOrganizationService(context.UserId); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parmameters. var entity = (Entity)context.InputParameters["Target"]; try { if (entity.Attributes.Contains("to") && entity.Attributes.Contains("subject")) { var recipients = entity.GetAttributeValue("to"); foreach (var party in recipients.Entities) { var partyName = party.GetAttributeValue("partyid").Name; var partyId = party.GetAttributeValue("partyid").Id; var partyType = party.GetAttributeValue("partyid").LogicalName; if (partyType == "contact") { var contactRec = service.Retrieve("contact", partyId, new ColumnSet(true)); entity["regardingobjectid"] = new EntityReference() { LogicalName = "contact", Id = partyId, Name = contactRec.GetAttributeValue("fullname") }; } } } } catch (Exception ex) { throw new InvalidPluginExecutionException("Error: " + ex.Message); } } } } } Источник: http://mscrmblog.net/2013/11/13/crm-...pient-address/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|