Check open activity related case using oData query

Sharing below code which returns some value using oData query. This function will check if there is any open activity related to Incident entity.

function CheckOpenActivity() {

var Id = parent.Xrm.Page.data.entity.getId();

var IncidentId = Id.substring(1, 37);

//Retrieve dynamically the organization’s server url

var serverUrl = document.location.protocol + “//” + document.location.host + “/” + Xrm.Page.context.getOrgUniqueName();

var ODATA_ENDPOINT = “/xrmservices/2011/OrganizationData.svc”;

var ODATA_EntityCollection = “/IncidentSet?$select=IncidentId,Incident_ActivityPointers/ActivityId,Incident_ActivityPointers/StateCode&$expand=Incident_ActivityPointers”;

var ODATA_Filter1 = “&$filter=IncidentId eq guid'” + IncidentId + “‘”;

var oDataRequestUrl = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Filter1;

var result = syncODataCall(oDataRequestUrl);

var isOpenActivity = ProcessReturnedEntities(result);

if (isOpenActivity)
alert(‘The Case has open Activity’);
else
alert(‘The Case has not any open Activity’);

}

// function to make synchronous oData call

function syncODataCall(odataSelect) {

var request = new XMLHttpRequest();

request.open(“GET”, odataSelect, false);

request.setRequestHeader(“Accept”, “application/json”);

request.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

request.send();

var objJQuery = jQuery.parseJSON(request.responseText);

return objJQuery.d

}

function ProcessReturnedEntities(d) {

var hasRelated = false;

if (d.results[0] != null) {

var activities = d.results[0].Incident_ActivityPointers;

if (activities != null) {

if (activities.results.length > 0) {

$.each(activities.results, function (index) {

if (activities.results[index].StateCode.Value == ‘0’) {

hasRelated = true;
return hasRelated;

}
});

} else {
hasRelated = false;
return hasRelated;
}

} else {
hasRelated = false;
return hasRelated;
}
}
return hasRelated;
}

 

Hope this help you.

Author: Arvind Singh

Solution Architect with 15+ years of exp. Dynamics CRM, Power Platform, Azure which includes Solution, Design, Development, Deployment, Maintenance and support experience.

2 thoughts on “Check open activity related case using oData query”

  1. It doesnt work for me, even after correcting all the quotation mark issues.
    I get the error “CheckOpenActivity is undefined”

    Like

  2. Of course right after I posted this, I fixed it. I missed one of the quotation marks.
    But I still got an error. The fist line with “parent.Xrm…” doesnt make sense. I removed the word “parent” and it now works.
    thanks!

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

%d bloggers like this: