【问题标题】:MS CRM 2011 Create Record and get it's GuidMS CRM 2011 创建记录并获取它的指南
【发布时间】:2023-03-08 13:20:01
【问题描述】:

我编写了一个用于创建记录的插件,但遇到以下问题:

我有 2 个实体。实体 1 和实体 2。 Entity2 与 Entity1 具有 n-1 关系,并且在其表单上还获得了 Entity1 的查找字段。在我的插件代码中,我创建了一个 Entity1 记录,然后我想创建一个 Entity2 记录,其中查找字段(到 Entity1)填充了刚刚创建的 Entity1 记录。但我得到了一个错误。我的代码如下所示:

Entity entity1 = new Entity("new_entity1");
//Here I'm filling my entity1's fields
Guid entity1Id = service.Create(entity1);

Entity entity2 = new Entity("new_entity2");
//Here I'm filling my entity2's fields
entity2 ["new_entity1id"] = new EntityReference("new_entity1",entity1Id);

service.Create(entity2);

我是 CRM 开发的新手,所以我无法真正认识到问题所在。我假设插件代码完成时可能会创建所有内容,因此在我创建 Entity2 记录时并未创建 Entity1 记录,但这只是我的假设。

任何帮助都会很棒。

错误信息:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx]]: An error occured in the AccountCreateHandler plug-inDetail:  <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">   <ErrorCode>-2147220891</ErrorCode>   <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <KeyValuePairOfstringanyType>
      <d2p1:key>OperationStatus</d2p1:key>
      <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
    </KeyValuePairOfstringanyType>   </ErrorDetails>   <Message>An error occured in the AccountCreateHandler plug-in</Message>   <Timestamp>2011-12-09T14:17:22.2773373Z</Timestamp>   <InnerFault i:nil="true" />   <TraceText>

[Execute_Buchen: Execute_Buchen.Class1] [dc2f71e8-b216-e111-b59f-00155d0a3339: Execute_Buchen.Class1: Update of new_entity0 START:IPlugin.Execute

</TraceText> </OrganizationServiceFault>

【问题讨论】:

  • 你能发布你得到的异常吗?你所拥有的是正确的。插件同步执行(前提是在插件注册工具上打勾)所以它会先创建实体 1,然后使用 guid 创建实体 2
  • 您是否使用了正确的实体名称和字段?在执行中它说Update of new_entity0 与您发布的代码相比?
  • 是的,我为这篇文章将其重命名为“new_entity0”。这也不是我的问题的实体之一(实体 1 或实体 2)。事实是,如果我删除该行: entity2 ["new_entity1id"] = new EntityReference("new_entity1",entity1Id);在我的问题上发布了记录,当然在 entity2 记录上使用空查找字段

标签: c# plugins dynamics-crm-2011


【解决方案1】:

尝试以这种方式代替。

entity2.Attributes.Add("new_entity1id",new EntityReference("new_entity1",entity1Id));

至于上述尝试后是否仍然抛出异常我只能想到两件事。

在entity2 ["new_entity1id"] = new EntityReference("new_entity1",entity1Id);行中

new_entity1id 是 entity2 的错误字段名称

或

该查找字段查找不同的实体并尝试保存您创建的EntityReference 会引发异常。

【讨论】:

  • 这里发布的两个原因中的任何一个在我看来都是引发异常的合适人选
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多