微软动态客户关系管理 2016

Microsoft Dynamics CRM 2016

我正在尝试编写一个提取 xml 以从 Facility/Equipment 实体检索 BusinessUnitID 和设备 ID,我已经用 C# 代码编写了这个提取 xml,但它抛出一个空值在行中引用 exception/System.NullReferenceException(粗体行) facility/equipment 实体中没有空值。 这是我的代码:

  private static OrganizationService _orgService;
  string fetchBU = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='equipment'>
        <attribute name='name' />
        <attribute name='equipmentid' />
        <order attribute='name' descending='false' />
      <filter type='and'>
         <condition attribute='businessunitid' operator='eq-businessid' />
      </filter>
    </entity>
   </fetch>";

EntityCollection ec = _orgService.RetrieveMultiple(new FetchExpression(fetchBU));
if (ec.Entities.Count > 0)
{
   Guid BusinessUnitId = (Guid)ec[0].Attributes["businessunitid"];
}

有人可以就此向我提出建议吗? 提前致谢!

您还需要在属性中添加 businessunitid,而不仅仅是在条件中:

string fetchBU = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
    <entity name='equipment'>
        <attribute name='name' />
        <attribute name='equipmentid' />
        <attribute name='businessunitid' />
        <order attribute='name' descending='false' />
      <filter type='and'>
         <condition attribute='businessunitid' operator='eq-businessid' />
      </filter>
    </entity>
   </fetch>";