0

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

2 回答 2

3

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

于 2012-06-13T09:10:13.663 回答
0

尝试将您的实体附加到 serviceContext。

http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.attach.aspx

于 2012-06-12T21:15:44.970 回答