- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2022 09:03 AM
Hello, I found a post in the forum showing how to populate the CAB date field via a Business Rule. But the script doesn't seem to work for me in either work or private instances. Is there something I am missing in the script? Many thanks for any assistance. Based on the start date, it should set the CAB date to the next Thursday.
REQUIREMENT: is to prevent a user setting a start date on a Change Request that is before the next CAB meeting, when the Risk is High. I thought f I could get the CBA date to populate on load, then I would have something to compare the Start Date to
BR Name - Set CAB Date
Table: Change Request
Advanced - Yes
When to Run - Before, Update
NO conditions
function onBefore(current, previous) {
setCabDate();
}
function setCabDate() {
var start = current.start_date;
var startDate = new Date (start.replace(/-/g, "\/"));
var nextThursday = getNextThursday(startDate);
var dateString = nextThursday.getFullYear() + "-" + ("0" + (nextThursday.getMonth() + 1)).slice(-2) + "-" + ("0" + (nextThursday.getDate())).slice(-2) + " 00:00:00";
var cabDate = new GlideDateTime(dateString);
alert(cabDate);
current.cab_date = cabDate;
}
function getNextThursday(date) {
var daysUntilThursday = 4 - date.getDay() ;
if(daysUntilThursday < 0){
daysUntilThursday+= 7;
}
date.setTime( date.getTime() + daysUntilThursday * 86400000 );
return date;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 02:16 PM
Hi
The cabDate being returned is a date and time value. The cab_date field is date only.
try using: current.cab_date = cabDate.getDate();
Updated script below:
(function executeRule(current, previous /*null when async*/ ) {
setCabDate();
function setCabDate() {
var start = current.start_date;
var startDate = new Date(start.replace(/-/g, "/"));
var nextThursday = getNextThursday(startDate);
var dateString = nextThursday.getFullYear() + "-" + ("0" + (nextThursday.getMonth() + 1)).slice(-2) + "-" + ("0" + (nextThursday.getDate())).slice(-2) + " 00:00:00";
var cabDate = new GlideDateTime(dateString);
current.cab_date = cabDate.getDate();
}
function getNextThursday(date) {
var daysUntilThursday = 4 - date.getDay();
if (daysUntilThursday < 0) {
daysUntilThursday += 7;
}
date.setTime(date.getTime() + daysUntilThursday * 86400000);
return date;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-11-2022 02:16 PM
Hi
The cabDate being returned is a date and time value. The cab_date field is date only.
try using: current.cab_date = cabDate.getDate();
Updated script below:
(function executeRule(current, previous /*null when async*/ ) {
setCabDate();
function setCabDate() {
var start = current.start_date;
var startDate = new Date(start.replace(/-/g, "/"));
var nextThursday = getNextThursday(startDate);
var dateString = nextThursday.getFullYear() + "-" + ("0" + (nextThursday.getMonth() + 1)).slice(-2) + "-" + ("0" + (nextThursday.getDate())).slice(-2) + " 00:00:00";
var cabDate = new GlideDateTime(dateString);
current.cab_date = cabDate.getDate();
}
function getNextThursday(date) {
var daysUntilThursday = 4 - date.getDay();
if (daysUntilThursday < 0) {
daysUntilThursday += 7;
}
date.setTime(date.getTime() + daysUntilThursday * 86400000);
return date;
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2022 12:07 PM
This is great, many thanks for the reply. I just need to do a client script now to compare the dates.