Execute js in synchronise mode

Repost with some minor change ..

One limit of the OData endpoint is that the response can only include up to 50 records, so if your result set has more records it’s necessary to reiterate the request using the url inside the __next property contained inside the returned object.

The following code shows how to fetch the request using synchronous calls, because sometimes you need (or want) to block the user 🙂

function IsRecordOpen(selectedValues) {
    
    var casesSelecteddata = Array.isArray(selectedValues) ? selectedValues.join(',') : selectedValues.replace("{", "").replace("}", "");
        
    if (casesSelecteddata != null) {
        try {

            var serverUrl;
            if (Xrm.Page.context.getClientUrl !== undefined) {
                serverUrl = Xrm.Page.context.getClientUrl();
            } else {
                serverUrl = Xrm.Page.context.getServerUrl();
            }
            var ODataPath = serverUrl + "/api/data/v9.2/";
            var incidentQueryUrl = ODataPath + "incidents?$select=statuscode&$filter=(incidentid eq " + casesSelecteddata + ")";
            
            var retrievedIncidents = GetODataRecords(incidentQueryUrl);

            if (retrievedIncidents != null) {
                if (retrievedIncidents.results[0].statuscode != null && retrievedIncidents.results[0].statuscode == 883990003) // CaseStatus is Closed
                {                    
                    return false;
                }
                else {                    
                    return true;
                }
            }
                        
        } catch (e) {
            // Log Error
        }
    }
}

function GetODataRecords(ODataUrl) {

    // we return an object with a similar structure as the OData endpoint
    var allRecords = new Object();
    allRecords.results = new Array();

    // we loop until we have an url to query
    var queryUrl = ODataUrl;
    while (queryUrl != null) {

        // we build the request
        var ODataRequest = new XMLHttpRequest();
        ODataRequest.open("GET", queryUrl, false); // false = synchronous request
        ODataRequest.setRequestHeader("Accept", "application/json");
        ODataRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        ODataRequest.send();

        if (ODataRequest.status === 200) {
            var parsedResults = JSON.parse(ODataRequest.responseText);
            if (parsedResults != null && parsedResults.value != null) {

                // we add the results to our object
                for (var i = 0; i < parsedResults.value.length; i++) {
                    allRecords.results.push(parsedResults.value[i]);
                }

                // check if there are more records and set the new url, otherwise we set to null the url
                if (parsedResults.__next != null) {
                    queryUrl = parsedResults.__next;
                } else {
                    queryUrl = null;
                }
            }
        } else {
            // if the request has errors we stop and return a null result
            queryUrl = null;
            allRecords = null;
        }
    }

    return allRecords;
}

Thanks!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s