Filtering data with the Dynamics 365 Web API

Calum Jacobs, 26 February 2021

In this blog, I will show you how to use the Xrm.WebApi.retrieveMultipleRecords endpoint, which I used to show or hide fields on a form based on whether a parent Account record had child Office records.

To do this I used the following query:

image

Now there are plenty of blogs explaining briefly how to use Xrm.WebApi.retrieveMultipleRecords, but none that I could find explaining how the $filter in the query worked.

Basically Xrm.WebApi.retrieveMultipleRecords is like a simplified version of Fetch XML. If you use advanced find to carry out a query, click Download Fetch XML, you will see something like the below.

image

image

The first parameter of the Xrm.WebApi.retrieveMultipleRecords endpoint is the entity you want to retrieve, which in our case is “mag_office”. We only want to select the “mag_officeid” field because we just want to know if there are any Offices associated to the Account. Next we apply a filter to associate the Office to the Account via it’s lookup field – the Office has a lookup to the Account called “mag_accountid” and the Account has a primary key called “accountid”.

image

So to summarise: Xrm.WebApi.retrieveMultipleRecords(A, ?$select=B &$filter=C/D eq GUID)

Where in the fetch XML:

A = schema name of entity you want to retrieve,

B = schema name of field you want to retrieve,

C = schema name of lookup field

D = schema name of primary key for lookup entity

GUID = lookup field GUID

image

Ie from the image above: Xrm.WebApi.retrieveMultipleRecords("mag_a,"?$select=mag_ aid &$filter=mag_Bid/bid eq " + GUID ").