CRM 2011 Online Plugin工作正常,但没有提交更新

时间:2012-12-04 17:30:52

标签: 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
{
    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;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:3)

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

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

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