我已经编写了一个包含以下配置的插件:
我只是想将一个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);
}
}
我得到的例外是:
未处理的例外情况: System.ServiceModel.FaultException`1 [Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk,Version = 6.0.0.0,Culture = neutral, PublicKeyToken = 31bf3856ad364e35]]:在。中发生错误 插件。详细信息:
-2147220891 OperationStatus 0 SubErrorCode -2146233088 插件中发生错误。
2015-01-15T05:34:00.1772929Z[PreValidationMarketingList.Plugins: PreValidationMarketingList.Plugins.PreValidateMarketingListCreate] [5454a088-749c-e411-b3df-6c3be5a83130:PreValidateMarketingListCreate]
进入 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute() 相关标识号:6d3ed105-f9c4-4006-9c80-08abd97c0140,发起用户: 5e1b0493-d07b-E411-b592-f0921c199288 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate是 触发实体:列表,消息:创建,关联ID: 6d3ed105-f9c4-4006-9c80-08abd97c0140,发起用户: 5e1b0493-d07b-e411-b592-f0921c199288退出 PreValidationMarketingList.Plugins.PreValidateMarketingListCreate.Execute() 相关标识号:6d3ed105-f9c4-4006-9c80-08abd97c0140,发起用户: 5e1b0493-d07b-E411-b592-f0921c199288
我做错了什么?在crm 2013中,如果两个字段都是日期时间,如何将一个字段设置为等于另一个字段?
答案 0 :(得分:6)
您不应该在此插件中调用Update
,因为您尚未创建并保存您尝试更新的记录。
首先,将其移至预操作,而不是预验证。由于在创建lastusedon
时验证不需要设置list
,所以这是一个非常合适的地方,但预操作确实是合适的。
我已经重新编写了代码以进行一些额外的检查:
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);
}
}
答案 1 :(得分:2)