我正在尝试使用以下代码创建设施/设备,但我收到的错误是"业务单位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");
}
提前致谢。
答案 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");
}