Using Web API Function in CRM 2016 Part 2

In our last article we discussed about standard functions, today we are going to discuss Query Function. These functions are basically conditional operators, so there is conditional operator corresponding to every query function. we can get complete list of query function reference from here. We can use these functions similar to standard function, but we just need to make sure to include full name of the function while using it.

Following is the sample code for using query function:

function GetNextMonthEvents() {
    var serverURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.0/him_events?$select=him_eventname&$filter=Microsoft.Dynamics.CRM.NextMonth(PropertyName='him_eventstartdate')", 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);
                alert(data.value.length);
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send();
}

In above sample code we are using NextMonth query function. This query function takes one parameter, which is name of the attribute that we want to validate. We are using this function to get list of event that is happening next month. So it will query our custom event entity and will display number of event which is scheduled next month.

Stay tuned for more Dynamics CRM updates!!

Leave a Reply

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