1

我是 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;
            }
        }

    }
}
4

1 回答 1

3

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

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

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

于 2012-12-04T18:39:22.700 回答