比较,添加和更新实体中的日期字段

时间:2012-06-12 20:56:23

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

尝试将实体的现有日期与当前日期进行比较。如果实体(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;
                    }
            }
        }

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)