Plugins CRM 2011在线插件工作正常,但未提交更新

Plugins CRM 2011在线插件工作正常,但未提交更新,plugins,dynamics-crm-2011,Plugins,Dynamics Crm 2011,我是CRM 2011的新开发人员。我已经为测试编写了简单的代码。 该插件似乎工作正常(通过ITracingService检查),但属性似乎没有采用新值 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk; namespace AgeUpdatePlugin {

我是CRM 2011的新开发人员。我已经为测试编写了简单的代码。
该插件似乎工作正常(通过ITracingService检查),但属性似乎没有采用新值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;

namespace AgeUpdatePlugin
{
    public class AgeUpdatePlugin:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            try
            {
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                if (tracingService == null)
                    throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                tracingService.Trace("Plugin has started..");

                //l'expression de condition
                ConditionExpression condition = new ConditionExpression("new_datenaissance", ConditionOperator.NotNull);

                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(condition);
                ColumnSet cols = new ColumnSet();
                //cols.AddColumn("Id");
                cols.AddColumn("new_candidatid");
                cols.AddColumn("new_age");
                cols.AddColumn("new_datenaissance");

                QueryExpression query = new QueryExpression();
                query.Criteria = filter;
                query.EntityName = "new_candidat";
                query.ColumnSet = cols;

                tracingService.Trace("appel retrieve multiple with date=" + DateTime.Now.ToString() + "/"+DateTime.Now.ToLocalTime().ToString());
                var retrieve = service.RetrieveMultiple(query).Entities;

                foreach (var c in retrieve)
                {

                    var dt =(DateTime)c.Attributes["new_datenaissance"];

                    tracingService.Trace(dt.ToString());
                    if (dt.Month == DateTime.Now.Month && dt.Day == DateTime.Now.Day)
                    {

                        tracingService.Trace((DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year).ToString());
                        c.Attributes["new_age"] = DateTime.Now.Year - DateTime.Parse(c["new_datenaissance"].ToString()).Year;
                        tracingService.Trace(c.Attributes["new_candidatid"].ToString() +" - "+c.Attributes["new_age"].ToString());
                        service.Update(c);

                        tracingService.Trace("updated");
                    }
                }
                tracingService.Trace("Plugin done working");
                throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created  "); 

            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

    }
}

每当您在插件中抛出异常时,事务就会中止,这样您就看不到应用的更改。删除以下行:

throw new InvalidPluginExecutionException("This is from a plugin that Mehdi has created  "); 

我假设您添加此项是为了查看跟踪输出,该输出仅在引发异常时可用。使用CRM Online,跟踪的最佳选择是写入自定义跟踪实体。

如果您可以将代码的大小减少到最小的测试用例,可能会有所帮助。谢谢Daz!你救了我的命:)