Hi there,
I am trying to return an warning message using Booking rules on the schedule board when a Resource requirement is manually scheduled and the Bookable resource it's being assigned to does not have the relevant characteristics to complete the requirement.
It is always returning the warning message even if the resource has the correct characteristics so I think I am missing something.
Here is my Jscript:
function newBookingRule(sbContext) {
var resourceSkills = [];
var jobSkills = [];
var ruleResult = {
IsValid: false,
Message: '',
Type: 'warning'
};
var resourceReqId = sbContext.newValues.ResourceRequirementId.replace(/[{}]/g, "");
if (resourceReqId != null) {
var req = new XMLHttpRequest();
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_requirementcharacteristics?$select=_msdyn_characteristic_value&$filter=_msdyn_resourcerequirement_value eq " + resourceReqId), false);
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.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.send();
if (req.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200) {
var results = JSON.parse(req.response);
for (var i = 0; i < results.value.length; i++) {
var _msdyn_characteristic_value = results.value[i]["_msdyn_characteristic_value"];
jobSkills.push({
skill: _msdyn_characteristic_value
});
}
}
}
}
var resourceId = sbContext.newValues.ResourceId.replace(/[{}]/g, "");
if (resourceId != null) {
var req2 = new XMLHttpRequest();
req2.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/bookableresourcecharacteristics?$select=_characteristic_value&$filter=_resource_value eq " + resourceId), false);
req2.setRequestHeader("Accept", "application/json");
req2.setRequestHeader("Content-Type", "application/json;charset=utf-8");
req2.setRequestHeader("OData-MaxVersion", "4.0");
req2.setRequestHeader("OData-Version", "4.0");
req2.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req2.send();
if (req2.readyState === 4) {
req2.onreadystatechange = null;
if (req2.status === 200) {
var results2 = JSON.parse(req2.response);
for (var j = 0; j < results2.value.length; j++) {
var _characteristic_value = results2.value[j]["_characteristic_value"];
resourceSkills.push({
skill: _characteristic_value
});
}
}
}
}
function check_characteristics(a, b) {
for (var i = 0, len = a.length; i < len; i++) {
for (var j = 0, len2 = b.length; j < len2; j++) {
if (a[i].skill === b[j].skill) {
b.splice(j, 1);
len2 = b.length;
}
}
}
}
check_characteristics(resourceSkills, jobSkills);
if (jobSkills.length == 0) {
ruleResult.IsValid = false;
ruleResult.Message = ("Resource Alert: Does not have required skills " );
ruleResult.Type = 'warning';
} else {
ruleResult.IsValid = true;
ruleResult.Message = "Success";
ruleResult.Type = 'success';
}
return ruleResult;
}
{
Xrm.Utility.getGlobalContext().saveSettingValue("msdyn_DisableProcessBookingRulesOnSaveBookingForm",false,).then(() => {a = "success"}, (error) => {a = error})
}
Please can you let me know what I am missing?
Thank you!
Rachel