MS CRM 2011创建记录并获取它的指南

本文关键字:获取 CRM 2011 创建 记录 MS | 更新日期: 2023-09-27 18:17:25

我写了一个用于创建记录的插件,我面临以下问题:

我有两个实体。Entity1和Entity2。Entity2与Entity1具有n-1关系,并且在其形式上也获得了对Entity1的查找字段。在我的插件代码中,我创建了一个Entity1记录,之后我要创建一个Entity2记录,其中lookupfield(到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开发的新手,所以我不能真正认识到问题。我假设也许一切都是在插件代码完成时创建的,所以Entity1记录不是在我创建Entity2记录时创建的,但这只是我的假设。

请帮忙就太好了。

错误信息:

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>

MS CRM 2011创建记录并获取它的指南

试试这样做。

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

如果在尝试了上面的方法后仍然抛出异常,我只能想到两件事。

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

一行

new_entity1identity2的错误字段名

该查找字段查找不同的实体,并且试图保存您创建的EntityReference时抛出异常。