Search code examples
dynamics-crmfetchxml

FetchXML attribute missing


When trying to retrieve an entity from Dynamics CRM using FetchXML one of the attributes appears to be missing.

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
  <entity name="sb_eventbooking">
    <attribute name="sb_name" />
    <attribute name="sb_bookeridname" />    < problem here
    <attribute name="createdon" />
    <atrribute ........

There are 18 attributes in the FetchXML file but when running the application only 17 are available:

enter image description here

And sb_bookeridname is missing. If I go into the FetchXML file and enter an attribue that I know doesn't exist then I get an error:

'sb_eventbooking' entity doesn't contain attribute with Name = 'fakeattribute'.

So the application accepts there is an attribue called 'sb_bookeridname' but I cannot get a value from it. I know there can be issues with columns with null values but other attributes don't seem to have this problem. I do use this check on all attributes and get values for all the other attributes:

if (entity.Attributes.Contains("sb_bookeridname") && entity.GetAttributeValue<String>("sb_bookeridname") != null)
{
     booking.bookeridname = entity.GetAttributeValue<String>("sb_bookeridname");
}

Edit 1:

enter image description here


Solution

  • I believe you have a lookup field with schema name: sb_bookerid. When we create a lookup field, CRM automatically creates a column in the table to store the text value corresponding to lookup. So when we create a lookup field sb_bookerid, then CRM will automatically create a column in the sb_eventbooking entity by the name sb_bookeridname.

    This is the reason you do not receive an error on executing FetchXML query because a column with the name exists but CRM restricts from showing its value. So if you want to retrieve the value in sb_bookerid field, please use following -

    if(entity.Contains("sb_bookerid"))
    {
    bookeridname=((EntityReference)entity.Attributes["sb_bookerid"]).Name
    }
    

    Hope it helps.