Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule to calculate the days pending

Annette Kitzmil
Tera Guru

Hello,

I have created a business rule that is supposed to fill a table column on my called Days Pending.  Then I created a Business rule with the script below that needs to calculate todays date minus the lastUpdate.  I need it to return just the number of days to my new column days_pending.  Can someone take a look at the script below and let me know what I am missing to have it prefill the number of days to my table column please?

 

(function executeRule(current, previous /*null when async*/) {

    var todaysDate = new GlideDate();
    var lastUpdate = new GlideDateTime(current.sys_updated_on);//This is using the "Updated" field
    var daysPendingCalculation = new GlideDate.subtract(todaysDate, lastUpdate);

    current.days = daysPendingCalculation;
   
})(current, previous);
9 REPLIES 9

James Chun
Kilo Patron

Thank you, I appreciate the share.

SanjivMeher
Mega Patron
Mega Patron

The subtract method returns result in microseconds. To get the  number of days, you need to divide it with 1000/number of secs/number if minutes/number of hours.

 
(function executeRule(current, previous /*null when async*/) {

    var todaysDate = new GlideDate();
    var lastUpdate = new GlideDateTime(current.sys_updated_on);//This is using the "Updated" field
    var daysPendingCalculation = new GlideDate.subtract(todaysDate, lastUpdate);

    current.days = daysPendingCalculation.getNumericValue() / 1000 / 60 / 60 / 24;
   
})(current, previous);

Please mark this response as correct or helpful if it assisted you with your question.

@Annette Kitzmil Did you try this solution?


Please mark this response as correct or helpful if it assisted you with your question.