Rahul J
Rahul J

Reputation: 103

Microsoft Dynamics CRM 2016

I am trying to write a fetch xml to retrieve BusinessUnitID and Equipment ID from Facility/Equipment Entity, i have written this fetch xml in c# code, but it throws a null reference exception/System.NullReferenceException in the line (BOLD'ed line) I don't any have null values in the facility/equipment entity. Here is my code:

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

enter image description here

Can someone please suggest me on this ? Thanks in advance!

Upvotes: 0

Views: 283

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

You need to add businessunitid also inside the attributes, not only in the condition:

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

Upvotes: 4

Related Questions