Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Booking Rule Javascript compare booking start time versus resource requirement start time

Posted on by 125

Hello all,

function ruleCheckWithinWorkHours(sbContext) {

    debugger;​

    var ruleResult = {​

        IsValid: false,​

        Message: '',​

        Type: 'error'​

    };​

    var resourceReqId = sbContext.newValues.ResourceRequirementId;​

    var requestedStartTime = null;​

var requestedEndTime = null;​

var requirementStartTime = null;​

var requirementEndTime = null;​

    var req = new XMLHttpRequest();​

    req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_resourcerequirements(" + resourceReqId + ")?$select=msdyn_timewindowstart, msdyn_timewindowend"), 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 result = JSON.parse(req.response);​

            requirementStartTime = result["msdyn_timewindowstart"];​

requirementEndTime = result["msdyn_timewindowend"];​

//compare requirementStartTime against booking startTime ​

requestedStartTime = sbContext.newValues.StartTime;​

requestedEndTime = sbContext.newValues.EndTime;​

if (requestedStartTime == requirementStartTime) {​

if (requestedEndTime <= requirementEndTime){​

if (requestedStartTime != null) {​

ruleResult.IsValid = true;​

ruleResult.Message = "Success";​

ruleResult.Type = 'success';​

alert("Requested Start Time: " + requestedEndTime + "   Requested End Time:" + requestedEndTime + "/n" + "Requirement Start Time: " + requirementStartTime + "   Requirement End Time:" + requirementEndTime );​

} ​

else {​

ruleResult.IsValid = false;​

ruleResult.Message = 'Requested time slot is not available';​

ruleResult.Type = 'error';​

}​

}​

}​

else {​

ruleResult.IsValid = false;​

ruleResult.Message = 'Requested time slot is not available';​

ruleResult.Type = 'error';​

} ​

        } ​

else {​

            Xrm.Utility.alertDialog(req.statusText);​

        }​

    }​

    return ruleResult;​

}​

​The highlighted "if" condition is never met because if I look at the browser console then the values of the variables are:

​requestedStartTime = Thu Jul 09 2020 09:00:00 GMT-0400 (Eastern Daylight Time) ​

requirementStartTime = "2020-07-09T14:00:00Z"​ <-- this is in GMT for some reason

msdyn_timewindowstart@OData.Community.Display.V1.FormattedValue: "7/9/2020 10:00 AM" <-- however, this value is correct and what was specified for the resource requirement

 

but I'm not clear on how to get the formatted value and then be able to compare the two strings since the date and time have to be the same format and timezone.

 

Thanks for your help,

Sudhir

  • Suggested answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Booking Rule Javascript compare booking start time versus resource requirement start time

    To format the date time fields you will need to use:

    • .toISOString();

    As for the requiredStartTime field, maybe check how the value is being populated and what the timezone format the field is?

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans