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
To format the date time fields you will need to use:
As for the requiredStartTime field, maybe check how the value is being populated and what the timezone format the field is?
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,818 Super User 2024 Season 2
Martin Dráb 229,147 Most Valuable Professional
nmaenpaa 101,156