CRM 2011从电子邮件CC列表中删除活动方

时间:2017-06-06 10:53:56

标签: c# plugins dynamics-crm dynamics-crm-2011 microsoft-dynamics

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

到目前为止我已经完成了,但我被困在了这之外。我的问题是电子邮件路由器在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字段继续保持数据发布创建。

我在Post Create Plugin中这样做。这样的方式是不是删除它们吗?我必须将其作为工作流程/ CWA吗?

2 个答案:

答案 0 :(得分:2)

而不是后期操作,请在Create plugin&amp;的预操作中注册它。从目标实体中删除欺骗CC部分列表。这将有效。

答案 1 :(得分:1)

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

select  count(*)

from   (select      custname

        from        table1

        where       date_time between date '2017-01-01' and date '2017-01-31'

        group by    custname

        having      count
                    (
                        case 
                            when    pages     in ('Summary' ,'Details') 
                                or  variable  in ('Complete','Receive')
                            then    1
                        end
                    ) > 0

                and count
                    (
                        case 
                            when    pages    not in ('Summary'  ,'Details') 
                                and variable not in ('Complete' ,'Receive')
                            then    1
                        end
                    ) = 0
        ) t