插件更新活动时的奇怪行为

时间:2014-05-05 13:15:57

标签: plugins dynamics-crm-2011

我正在创建一个插件,用于创建电话会话活动以更新收件人联系人实体中的字段。此插件在创建和更新电话呼叫活动时运行。

在创建AND“new_targetfield”为null =>正确更新

更新和“new_targetfield”not null =>正确更新

更新时,“new_targetfield”为null =>没有任何事情发生

我尝试运行Plugin Profiler但我一直收到错误:

 <ErrorCode>-2147220970</ErrorCode>
 <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic

以下是我的代码的一部分:

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity entity = context.InputParameters["Target"] as Entity;

            if (entity.LogicalName != "phonecall")
            {
                return;
            }


            DateTime activitydate=entity.GetAttributeValue<DateTime>("actualstart");
            if (activitydate ==null && context.MessageName =="Update")
            {
                activitydate=((Entity)context.PostEntityImages["PhoneCallPostImage"]).GetAttributeValue<DateTime>("actualstart");
            }


            if (activitydate != null)
            {

                // update recipients
                EntityCollection Recipients = entity.GetAttributeValue<EntityCollection>("to");

                if (Recipients == null && context.MessageName == "Update")
                {
                    Recipients = ((Entity)context.PostEntityImages["PhoneCallPostImage"]).GetAttributeValue<EntityCollection>("to");

                }

                if (Recipients != null)
                {
                    foreach (Entity recipient in Recipients.Entities)
                    {
                        EntityReference partyId = recipient.GetAttributeValue<EntityReference>("partyid");

                        if (partyId != null)
                        {

                            // get recipient id
                            if (partyId.LogicalName == "contact")
                            {
                                Guid contactid = partyId.Id;

                                // update the recipient Last Contacted with Phone call date 
                                string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='contact'>
                                    <attribute name='contactid' />
                                    <attribute name='new_targetfield' />
                                    <filter type='and'>
                                      <condition attribute='contactid' operator='eq' uitype='contact' value='"+contactid+@"' />
                                    </filter>
                                  </entity>
                                </fetch>";

                                EntityCollection result = service.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(fetch));
                                if (result.Entities.Count > 0)
                                {
                                    DateTime lasttouched = result.Entities[0].GetAttributeValue<DateTime>("new_targetfield");
                                    if (lasttouched != null)
                                    {
                                        if (activitydate > lasttouched)
                                        {
                                            Entity contact = new Entity();
                                            contact.LogicalName = "contact";
                                            contact.Id = contactid;
                                            contact.Attributes = new AttributeCollection();
                                            contact.Attributes.Add("new_targetfield", activitydate);

                                            service.Update(contact);
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        Entity contact = new Entity();
                                        contact.LogicalName = "contact";
                                        contact.Id = contactid;
                                        contact.Attributes = new AttributeCollection();
                                        contact.Attributes.Add("new_targetfield", activitydate);

                                        service.Update(contact);
                                    }



                                }

                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }

                    }
                }

1 个答案:

答案 0 :(得分:0)

问题是DateTime变量永远不会是null,而是等于MinValue的{​​{1}}。这就是为什么部分代码没有接近的原因。