web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Suggested answer

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

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    Thomas David Dayman Profile Picture
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 46 Super User 2025 Season 2

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 46

#3
CU23101130-0 Profile Picture

CU23101130-0 17

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans