To achieve that, we have developed a JScript function that uses OData query to retrieve all activities related to the current record and then loop over the result set and check the state of each.
Note: the following scripts are not tested with UR12 (and later). With the big changes introduced with this update, it could not work "as is".
function hasRelatedOpenActivities() {
//Retrieve
dynamically the organization's server url
var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var entityNameSet = Xrm.Page.data.entity.getEntityName() + 'Set'; //Entity Name
var entityIdAttr = Xrm.Page.data.entity.getEntityName() + 'Id'; //Entity ID field name
var hasRelated = false;
//Related activities are retrieved by using the relationship named "Entity name"_ActivityPointers
var oData = "/" + entityNameSet + "?$select=" + entityIdAttr + "," + Xrm.Page.data.entity.getEntityName() + "_ActivityPointers&$expand=" + Xrm.Page.data.entity.getEntityName() + "_ActivityPointers&$filter=" + entityIdAttr + " eq guid'" + Xrm.Page.data.entity.getId() + "'";
var oPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var request = new XMLHttpRequest();
request.open("GET", oPath + oData, false); //Synchronous operation
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
var reqResult = JSON.parse(request.responseText).d;
if (reqResult.results[0] != null) {
var activities = eval('reqResult.results[0].' + Xrm.Page.data.entity.getEntityName() + '_ActivityPointers');
if (activities != null) {
if (activities.results.length > 0) {
$.each(activities.results, function (index) {
if (activities.results[index].StateCode.Value == '0') {
hasRelated = true;
}
});
} else {
hasRelated = false;
}
} else {
hasRelated = false;
}
}
} else {
//Handle errors
}
}
};
request.send();
return hasRelated;
}
Hope it can be useful.
Happy CRM coding.
var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var entityNameSet = Xrm.Page.data.entity.getEntityName() + 'Set'; //Entity Name
var entityIdAttr = Xrm.Page.data.entity.getEntityName() + 'Id'; //Entity ID field name
var hasRelated = false;
//Related activities are retrieved by using the relationship named "Entity name"_ActivityPointers
var oData = "/" + entityNameSet + "?$select=" + entityIdAttr + "," + Xrm.Page.data.entity.getEntityName() + "_ActivityPointers&$expand=" + Xrm.Page.data.entity.getEntityName() + "_ActivityPointers&$filter=" + entityIdAttr + " eq guid'" + Xrm.Page.data.entity.getId() + "'";
var oPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var request = new XMLHttpRequest();
request.open("GET", oPath + oData, false); //Synchronous operation
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
var reqResult = JSON.parse(request.responseText).d;
if (reqResult.results[0] != null) {
var activities = eval('reqResult.results[0].' + Xrm.Page.data.entity.getEntityName() + '_ActivityPointers');
if (activities != null) {
if (activities.results.length > 0) {
$.each(activities.results, function (index) {
if (activities.results[index].StateCode.Value == '0') {
hasRelated = true;
}
});
} else {
hasRelated = false;
}
} else {
hasRelated = false;
}
}
} else {
//Handle errors
}
}
};
request.send();
return hasRelated;
}
Hope it can be useful.
Rate this posting:
{[['']]}
No comments:
Post a Comment