创建插件的预验证

Prevalidation on create plugin

我写了一个配置如下的插件:

我只是想将一个 datetime 字段设置为等于另一个 datetime 字段:

 IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                try
                {
                    if (entity.LogicalName == "list" && entity.Attributes["gbs_lastusedonoriginal"] != null)
                    {
                        entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"];
                        service.Update(entity);
                    }
                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex);
                }

            }

我得到的异常是:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occured in the plug-in.Detail:
-2147220891 OperationStatus 0 SubErrorCode -2146233088 An error occured in the plug-in.
2015-01-15T05:34:00.1772929Z

[PreValidationMarketingList.Plugins: PreValidationMarketingList.Plugins.PreValidateMarketingListCreate] [5454a088-749c-e411-b3df-6c3be5a83130: PreValidateMarketingListCreate]

Entered PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute(), Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate is firing for Entity: list, Message: Create, Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288 Exiting PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute(), Correlation Id: 6d3ed105-f9c4-4006-9c80-08abd97c0140, Initiating User: 5e1b0493-d07b-e411-b592-f0921c199288

我做错了什么?在 crm 2013 中,如果两个字段都是日期时间,我如何将一个字段设置为等于另一个字段?

您不应在此插件中调用 Update,因为您尚未创建并保存要更新的记录。

首先,将其移至预操作,而不是预验证。这是一个小问题,但操作前确实是合适的地方,因为 list.

的创建验证不需要设置 lastusedon

我重新编写了您的代码以进行一些额外的检查:

        IPluginExecutionContext context = localContext.PluginExecutionContext;
        IOrganizationService service = localContext.OrganizationService;

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];
            try
            {
                if (entity.LogicalName == "list" && entity.Attributes.Contains("gbs_lastusedonoriginal") && entity["gbs_lastusedonoriginal"] != null)
                {
                    if (entity.Attributes.Contains("lastusedon") )
                        entity.Attributes["lastusedon"] = entity.Attributes["gbs_lastusedonoriginal"];
                    else entity.Attributes.Add("lastusedon", entity.Attributes["gbs_lastusedonoriginal"];                        
                }
            }
            catch (FaultException ex)
            {
                throw new InvalidPluginExecutionException("An error occured in the plug-in.", ex);
            }

        }

在 MSDN 上查看 this link。在 Pre-Event 中,记录尚未保存在 SQL 数据库中。您可以从 InputParameters 修改实体对象。在事件前插件之后,将使用您修改的属性创建记录。

Pre-validation和Pre-operation的主要区别在于Pre-operation阶段是在数据库事务中执行的,而Pre-validation不是。有关详细信息,请参阅 MSDN