CRM 2011插件有效但出错

时间:2013-08-30 08:26:23

标签: dynamics-crm-2011 dynamics-crm

我正在开发一个CRM 2011插件,如果用户停用该帐户,该插件会更改帐户实体的一个字段值。我花了很多时间想知道什么是错的,因为每次我停用某个帐户时都会收到以下错误

“错误。发生了错误。请再次尝试此操作。如果问题仍然存在,请检查Microsoft Dynamics CRM社区以获取解决方案,或与您组织的Microsoft Dynamics CRM管理员联系。最后,您可以与Microsoft支持部门联系”

但过了一段时间后,我注意到即使错误我的插件实际上工作得很好。我的代码以下是以防万一(注意我们称我们的帐户为客户)

Entity client = (Entity)context.InputParameters["Target"];

OptionSetValue state = (OptionSetValue)client["statecode"];

if (state.Value == 1)
{
    OptionSetValue clientStatus = new OptionSetValue(100000000);
    client["customertypecode"] = clientStatus;                   
    service.Update(client);
}

所有人都有任何想法会导致这个问题吗?如果我禁用我的插件,然后停用任何帐户,它可以正常工作,没有任何错误。

我的插件在预操作阶段同步注册。

提前谢谢!

2 个答案:

答案 0 :(得分:0)

那么你的插件是在SetStateDynamic消息的预操作中注册的吗?你想要做的就是更新客户规范?我的猜测是,因为你没有显示你的代码,所以你没有从插件上下文中获得IOrganizationService。

答案 1 :(得分:0)

当您的插件订阅SetStateSetStateDynamicEntity消息时,该实体不在IPluginExecutionContext.InputParameters["Target"]
这些消息有三个InputParameters:

  • “EntityMoniker”(EntityReference)
  • “州”(OptionSetValue)
  • “状态”(OptionSetValue)

所以没有“目标”。

EntityReference clientRef = context.InputParameters["EntityMoniker"] as EntityReference;
OptionSetValue newStateCode = context.InputParameters["State"] as OptionSetValue;

if (newStateCode.Value == 1)
{
    Entity updateClient = new Entity(clientRef.LogicalName);
    updateClient.Id = clientRef.Id;
    updateClient["customertypecode"] = new OptionSetValue(100000000);

    service.Update(updateClient);
}

当您的插件订阅Update消息时:

由于您处于预操作阶段且目标实体是您要更新的实际实体,为什么要调用service.Update?只需将属性添加到目标实体并完成它...

Entity client = (Entity)context.InputParameters["Target"];

OptionSetValue state = (OptionSetValue)client["statecode"];

if (state.Value == 1)
{
    OptionSetValue clientStatus = new OptionSetValue(100000000);
    client["customertypecode"] = clientStatus;
}