Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Unanswered

Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

(0) ShareShare
ReportReport
Posted on by 5

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&amp;$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 

  • Misssayer Profile Picture
    Misssayer 5 on at
    RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    Hi all,

    I have had help from the original author of the Jscript - Thomas - and he has sent me a revised version of the code which now works successfully. See below if this will help anyone else!

    function newBookingRule4(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 = true;

    ruleResult.Message = "Success";

    ruleResult.Type = 'success';

    } else {

    ruleResult.IsValid = false;

    ruleResult.Message = ("Resource Alert: Does not have required skills " );

    ruleResult.Type = 'warning';

    }

    return ruleResult;

    }

  • Misssayer Profile Picture
    Misssayer 5 on at
    RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    I've also just tried spinning up a new trial environment to see if it worked without the changes and it doesn't appear to work there either.

    Could you please send me the code you have entered?

    Thank you so much!

  • Misssayer Profile Picture
    Misssayer 5 on at
    RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    It did have the amp in however i've taken that out and tested and still the same outcome.

    We have renamed some of the tables as follows:

    msdyn_characteristic - Skills and Certifications

    msdyn_resourcerequirement - Visit Characteristics

    msdyn_requirementcharacteristic - Requirement Skills and Certifications

    Could this be making a difference? I may have wrongly assumed it was using the Name rather than Display name?

    Thank you.

  • RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    That's Interesting. 

    I copied your code and created a booking rule and its working as expected. The only thing I changed was that I removed the "amp;" from the GET query for both Bookable Resource Characteristics & Requirement Charestistics as the non rich text editor automatically adds it. 

    "/api/data/v8.2/bookableresourcecharacteristics?$select=_characteristic_value&amp;$filter=_resource_value eq "

    The system does not retrieve the records if we keep the "amp;" 

    pastedimage1648726436410v1.png

    Can you check if the amp; is present in your code. That the only thing I can think of right now is that the queries are not retrieving the records and as a result the if (jobSkills.length == 0)  is always true. 

     

  • Misssayer Profile Picture
    Misssayer 5 on at
    RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    Hi  Prathamesh,

    Thanks for the comment, I have tried this (see below) and it then never shows the warning message as it always thinks the skills match.

    Anything else I can try?

    Thanks,

    function newBookingRule4(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&amp;$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&amp;$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 = true;
    ruleResult.Message = "Success";
    ruleResult.Type = 'success';
    } else {
    ruleResult.IsValid = false;
    ruleResult.Message = ("Resource Alert: Does not have required skills " );
    ruleResult.Type = 'warning';
    }
    return ruleResult;
    }

  • RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    Hi Misssayer,

    Can you try reversing the if (jobSkills.length == 0) contents as follows ?

    if (jobSkills.length == 0) {

       ruleResult.IsValid = true;

       ruleResult.Message = "Success";

       ruleResult.Type = 'success';

    } else {

       ruleResult.IsValid = false;

       ruleResult.Message = ("Resource Alert: Does not have required skills ");

       ruleResult.Type = 'warning';

    }

    I believe the reason is because the code is splicing the length when the skills match. As a result, the length is 0 and the warning message is shown on for the booking. 

    So reversing the code should mark the booking rule as success when the skills of the resource and the requirement match. 

    Regards,

    Prathamesh

  • RE: Booking Rule - Comparing the characteristics of resource requirements and bookable resources with Jscript

    Hi Rachel, here are a few good links with samples - dyn365apps.com/.../

    pragmaticdevs.wordpress.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans