【问题标题】:Comparing, Adding, and Updating date field in an entity比较、添加和更新实体中的日期字段
【发布时间】:2012-06-12 20:56:22
【问题描述】:

尝试将实体的现有日期与当前日期进行比较。如果实体(testentity)日期的实体字段(testfield)在当前日期之后等于OR,则在该字段的日期上加1年。

问题 - 出于某种原因,它会读取所有日期并进行比较,但不会在现场更新。我在实体上使用了后期操作步骤。

更新:我添加了ServiceContext.UpdateObject(entity)和ServiceContext.SaveChanges();到代码,但现在它给了我“上下文当前没有跟踪......”错误。

任何帮助将不胜感激。谢谢!

请看下面的代码。

   public class PostUpdate: Plugin
{

    public PostUpdate()
        : base(typeof(PostUpdate))
    {
        base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_testentity", new Action<LocalPluginContext>(ExecutePostUpdate)));

      protected void ExecutePostupdate(LocalPluginContext localContext)
    {
        // get the plugin context 
        IPluginExecutionContext context = localContext.PluginExecutionContext;

        //Get the IOrganizationService
        IOrganizationService service = localContext.OrganizationService;

        //create the service context
        var ServiceContext = new OrganizationServiceContext(service);
        ITracingService tracingService = localContext.TracingService;

        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];


            // Verify that the target entity represents an account.
            // If not, this plug-in was not registered correctly.
            if (entity.LogicalName != "new_testentity")
                return;

                    try
                    {
                        var k = entity["new_testfield"];
                        DateTime m = Convert.ToDateTime(k);

                        DateTime d = DateTime.Now;

                        int result = DateTime.Compare(m, d);

                        // compare the dates 
                        if (result <= 0)
                        {
                            try
                            {


                                entity["new_testfield"] = DateTime.Now.AddYears(1);
                                ServiceContext.UpdateObject(entity);
                            }
                        ServiceContext.SaveChanges();
                  //Adding this is giving me "The context is not currently tracking                    the 'new_testentity' entity."

                            }
                            catch (FaultException<OrganizationServiceFault> ex)
                            {
                            }
                        }
                    }

                    //<snippetFollowupPlugin3>
                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
                    }
                    //</snippetFollowupPlugin3>

                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                        throw;
                    }
            }
        }

【问题讨论】:

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


    【解决方案1】:

    您应该在操作前步骤注册您的插件,然后只需在InputParameter PropertyBag 中添加/更改适当的值。这样,您的更改与事务内联,您不需要单独的更新调用。

    【讨论】:

    • 格雷格,非常感谢您的意见。我在预操作步骤中注册了插件。 'if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // 从输入参数中获取目标实体。实体实体 = (Entity)context.InputParameters["Target"];'应该在这之后吗?我还应该在这里添加条件吗?抱歉,我对如何做到这一点有点迷茫。非常感谢您的帮助!
    • 你指的是雷诺在这篇文章中的评论吗? stackoverflow.com/questions/8167571/…
    • @cashbowl:找出答案的一种方法 - 试试看!但该解决方案应该适合您。
    • 我做到了,彼得。但是即使逻辑正确,字段也不会更新。
    • 如果您的插件处于预运行阶段,则将其添加到执行工作的分支中,假设“new_testfield”存在并且是DateTime类型。您不需要 ServiceContext.UpdateObject 或 ServiceContext.SaveChanges。 var newValue = DateTime.Now.AddYears(1); if(entity.Contains("new_testfield")){entity["new_testfield"] = newValue;}else{entity.Attributes.Add("new_testfield",newValue);}
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2015-12-01
    • 2012-03-15
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    相关资源
    最近更新 更多