Execute Predefined Queries using Dynamics CRM Web API

In Dynamics CRM 2016 Web API we can execute predefined queries or retrieve data by executing custom fetch XML. Users can create views or save the queries in Advanced Find. Both these queries represent predefined queries, you can retrieve and execute using the Dynamics CRM Web API.

There are two types of predefined queries available.

Saved Query

Saved queries represents system-defined views for an entity. These views are stored in the savedquery EntityType.

User Query

User queries are Advanced Find searches saved by users for an entity. These views are stored in the userquery EntityType.

 

Queries in Dynamics CRM Web API 1

 

 

Both type of predefined queries contain FetchXML definition for the data to return. You can get the savedqueryid by executing the following request and after that you can execute the query by passing the savedqueryid value. For example, to execute the Active Accounts saved query, you have to get the primary key using a query like this.

GET [Organization URI]/api/data/v8.0/savedqueries?$select=name,savedqueryid&$filter=name eq 'Active Accounts'

You can then use the savedqueryid value and pass it as the value to the savedQuery parameter to the accounts entity set.

GET [Organization URI]/api/data/v8.0/accounts?savedQuery=00000000-0000-0000-00aa-000010001002

Use the same approach to get the userqueryid and pass it as the value to the userQuery parameter to the entity set that matches the corresponding returnedtypecode of the saved query.

Get the saved user query:

GET [Organization URI]/api/data/v8.0/userqueries?$select=name,userqueryid&$filter=name eq 'Fourth Coffee Opportunities'


To run this query use below request:

GET [Organization URI]/api/data/v8.0/accounts?userQuery=121c6fd8-1975-e511-80d4-00155d2a68d1

Example

We are going to execute user define query here as an example.

Following query will return all won Opportunities related to “Fouth Cofee(sample)” account.

Queries in Dynamics CRM Web API 2

Save the query by clicking on save button and give appropriate name.

Queries in Dynamics CRM Web API 3


First step is to get the userqueryid of this saved query.

You can get userqueryid of this query by executing this request:

GET [Organization URI]/api/data/v8.0/userqueries?$select=name,userqueryid&$filter=name eq 'Fourth Coffee Won Opportunities'  

The next step is to execute the query by using the userqueryid we got by executing the above request.

The predefined User query is related to Opportunity Entity so the request to execute the query, where 121c6fd8-1975-e511-80d4-00155d2a68d1 is the userqueryid.

Request:

GET [Organization URI]/api/data/v8.0/Opportunity?userQuery=121c6fd8-1975-e511-80d4-00155d2a68d1 

Code

You can use the following code to run the user query.


function executeUserQuery() {
 var clientUrl = Xrm.Page.context.getClientUrl();
 var req = new XMLHttpRequest()
 req.open("GET", encodeURI(clientUrl + "/api/data/v8.0/accounts?userQuery=121c6fd8-1975-e511-80d4-00155d2a68d1"), true);
 req.setRequestHeader("Accept", "application/json");
 req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 req.setRequestHeader("OData-MaxVersion", "4.0");
 req.setRequestHeader("OData-Version", "4.0");
 req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
 req.onreadystatechange = null;
 if (this.status == 200) {
 var data = JSON.parse(this.response);
 var dat = data.value;
 for (var i = 0; i < dat.length; i++) {
 alert("Opportunity Name : "+dat[i].name);
 }
 }
 else {
 var error = JSON.parse(this.response).error;
 alert(error.message);
 }
 }

};
req.send();
}



1 Comment

Leave a Reply

*

code