Tip #797: When expanding, bring the id with you

IdentityThe week of “just read your mailbox and publish what other smart people found”, continues. Derek Finlinson has the microphone:

Ran into an interesting issue with the Web API and expanding navigation properties today that I thought I would pass along.

I needed to retrieve an attribute from a lookup value related to an entity so ran this lovely query (details changed obviously) (we tried – definitely fake org – t.j.):

https://fakeorg.crm.dynamics.com/api/data/v8.1/msdyn_workorderproducts?$select=msdyn_workorderproductid&
$expand=msdyn_workorderincident($select=new_customfield)

It worked great when new_customfield contained data. However, if it was null, the msdyn_workorderincident entity itself came back as null, even though the work order product was in fact related to an incident. To get around this, just make sure to always include the id field in your expanded $select (msdyn_workorderincidentid in this case).

https://fakeorg.crm.dynamics.com/api/data/v8.1/msdyn_workorderproducts?$select=msdyn_workorderproductidid&
$expand=msdyn_workorderincident($select=new_customfield,msdyn_workorderincident)

That way you still get your expanded entity returned with a null new_customfield. Not sure if this is common knowledge but was new to me. Hopefully it helps someone else out.

Leave a Reply

Your email address will not be published. Required fields are marked *