1

如何通过插件从CRM 2011中的电子邮件CC字段删除活动方?CRM 2011从电子邮件CC列表中删除活动方

到目前为止我已经完成了,但是我超出了这个范围。我的问题是电子邮件路由器使用CC字段中的同一电子邮件地址复制联系人。我们建议删除联系人是最好的解决方案,但该解决方案尚未被接受。因此,现在我们需要从电子邮件抄送字段中删除活动方,并将其替换为未解决的电子邮件地址。

有什么想法?

Email email = crmService.Retrieve(Email.EntityLogicalName, emailId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)).ToEntity<Email>(); //Do not use ColumnSet(true) 
List<ActivityParty> ccPartyList = email.Cc.ToList<ActivityParty>(); 
List<string> ccEmailAddresses = new List<string>(); 

foreach (ActivityParty ap in ccPartyList) 
{ 
    ccEmailAddresses.Add(ap.AddressUsed); 
} 

List<string> dedupedCCEmailAddresses = ccEmailAddresses.Distinct().ToList(); 
List<string> emailAddressToBeRemoved = new List<string>(); 


//Check for each unique email addresses, how many records are there 
foreach (string emailAddress in dedupedCCEmailAddresses) 
{ 
    int count = ccPartyList.Count(ap => ap.AddressUsed.Equals(emailAddress, StringComparison.InvariantCultureIgnoreCase)); 
    if (count > 1) //Same email address; Multiple Records 
    { 
     emailAddressToBeRemoved.Add(emailAddress); 
    } 
} 

//Remove ALL Activity Party from the List 
foreach (string emailAddress in emailAddressToBeRemoved) 
{ 
    ccPartyList.RemoveAll(ap => ap.AddressUsed.Equals(emailAddress, StringComparison.InvariantCultureIgnoreCase)); 
    ActivityParty unResolvedEmailAddress = new ActivityParty(); 
    unResolvedEmailAddress.AddressUsed = emailAddress; 
    ccPartyList.Add(unResolvedEmailAddress); 
} 

Email emailToUpdate = new Email(); 
emailToUpdate.Id = emailId; 
emailToUpdate.Cc = ccPartyList; 
crmService.Update(emailToUpdate); 

我现在用下面的代码更新了它,其中我删除了所有CC方。

if (localContext == null) 
{ 
    throw new ArgumentNullException("localContext"); 
} 

IPluginExecutionContext context = localContext.PluginExecutionContext; 
IOrganizationService service = localContext.OrganizationService; 
tracingService = localContext.TracingService; 

Email emailFromTarget = (context.InputParameters.Contains("Target") 
         && context.InputParameters["Target"] is Entity) 
         ? 
((Entity)context.InputParameters["Target"]).ToEntity<Email>() : null; 

Entity email = service.Retrieve(Email.EntityLogicalName, emailFromTarget.Id, new ColumnSet("cc")); 

EntityCollection cc = email.GetAttributeValue<EntityCollection>("cc"); 
if (cc != null) 
{ 
    cc.Entities.ToList().ForEach(party => 
    { 
     cc.Entities.Remove(party); 
    }); 
} 

email["cc"] = cc; 
service.Update(email); 

即使如此,CC领域继续坚持数据创建后。

我正在做后创建插件。这是不是将它们删除?我是否必须将其作为Workflow/CWA来执行?

+1

为什么发布创建?你尝试过Pre操作吗? –

+0

@ArunVinoth是的,我做过Pre Pre Operate。我将稍后发布完整的代码。 – Kanini

+0

@ArunVinoth您可以将此作为答案来写,因此我可以将其标记为已接受? – Kanini

回答

1

相反操作后,在创建插件&移除目标实体的欺骗CC partylist的术前注册。这将工作。

1

您建议的代码有什么问题?

// Get your cc field 
var cc = email.GetAttributeValue<EntityCollection>("cc"); 

// Iterate through the collection. If there's a partyId, it's a party list, so remove it 
cc.Entities.ToList().ForEach(entity => 
{ 
    var partyId = entity.GetAttributeValue<EntityReference>("partyid"); 
    if (partyId != null) 
     cc.Entities.Remove(entity); 
} 

// Update your email 
email["cc"] = cc; 
service.Update(email); 
+0

我现在已经用完整的代码示例更新了这个问题。从插件尝试上述逻辑不起作用,但在控制台应用程序中正常工作。 – Kanini

相关问题