尝试创建设备记录时,无法将业务单位标识设置为空

时间:2016-04-18 07:25:54

标签: c# dynamics-crm dynamics-crm-2016

我正在尝试使用以下代码创建设施/设备,但我收到的错误是"业务单位ID无法设置为NULL"

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

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");
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

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

您无法将Guid分配给EntityReference属性。修改您的代码类似于:

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");
}