Set CAB Date field on Change Form via Business Rule - not working

Baggies
Kilo Guru

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;   
} 
    

 

1 ACCEPTED SOLUTION

Jon23
Mega Sage

Hi @Baggies 

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);

View solution in original post

2 REPLIES 2

Jon23
Mega Sage

Hi @Baggies 

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);

This is great, many thanks for the reply. I just need to do a client script now to compare the dates.