1

我试图使用以下代码创建设施/设备,但我收到一条错误消息,提示“业务单位 ID 不能设置为 NULL”

我在我的 crm 2016 中只创建了一个业务部门这是我的代码:

string fetchBU = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='businessunit'>
                                    <attribute name='name' />
                                    <attribute name='address1_telephone1' />
                                    <attribute name='websiteurl' />
                                    <attribute name='parentbusinessunitid' />
                                    <attribute name='businessunitid' />
                                    <order attribute='name' descending='false' />
                                  </entity>
                                </fetch>";

EntityCollection resultequipments = _orgService.RetrieveMultiple(new FetchExpression(fetchBU));

if (resultequipments.Entities.Count > 0)
{
    Guid BuId;
    BuId = (Guid)resultequipments.Entities[0]["businessunitid"];
    Entity equipment = new Entity("equipment");
    equipment["name"] = "test";
    equipment["businessunitid"] = BuId;
    _orgService.Create(equipment);
    Console.WriteLine("Facility successfully created");
}

提前致谢。

4

1 回答 1

1

查询结果中的businessunitid属性是主键,即Guid类型。

实体中的businessunitid属性equipment是一个查找,它由一个EntityReference对象表示。

您不能将 a 分配GuidEntityReference属性。修改类似这样的代码:

EntityCollection businessUnits = _orgService.RetrieveMultiple(new FetchExpression(fetchBU));

if (businessUnits.Entities.Count > 0)
{
    Entity equipment = new Entity("equipment");
    equipment["name"] = "test";
    equipment["businessunitid"] = new EntityReference("businessunit", businessUnits.Entities[0].Id);
    _orgService.Create(equipment);
    Console.WriteLine("Facility successfully created");
}
于 2016-04-18T12:18:10.387 回答