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 addDaysLocalTime() returning undefined

jimnicholson
Giga Guru

Hi All,

Hopefully I am missing something very obvious, or I am misusing the addDaysLocalTime() function, but I have been spinning my wheels on this for hours, and I can't seem to get it to work.

On a change request, I have a business rule set to run when the change hits a particular state.   Check, working fine.

I have a script set to run when the state is hit, basically, I want to get the start date, find out if it's a weekday, and if it's not, subtract one day from the start date field and populate another field with that value, otherwise populate it with the start date (the date part only).   I can evaluate the day of the week no problem, everything returns fine here.   Where I am having the problem is when I try to take   day away from the start date and return that value.   I am constantly getting "undefined" (I changed the field to a text field so I could see the results):

function onBefore(current, previous) {

var sDt = new GlideDateTime(current.start_date);

var dayOfWeek = sDt.getDayOfWeekLocalTime();

var datePart = sDt.getLocalDate().getByFormat('MM-dd-yyyy');

var timePart = sDt.getLocalTime().getByFormat('hh:mm:ss');

var one = sDt.addDaysLocalTime(-1);

var oneDay = one.getLocalDate().getByFormat('MM-dd-yyyy');

if (dayOfWeek >=1 && dayOfWeek <=5){

        current.eow = datePart + ' 08:30:00 AM' ;

}

else {

current.eow = oneDay + ' 08:30:00 AM';

}

}

My results are:

for   current.eow = datePart + ' 08:30:00 AM' ; I get 01-21-2016 08:30:00 AM   (perfect for a change that starts on 01/21/2016)

for current.eow = oneDay + ' 08:30:00 AM'; I get "undefined 08:30:00 AM"

(**Note, EOW is a string field for now)

I've got to be doing something wrong here, please tell me I am....

Thanks for any help you may be able to offer.

Jim

1 ACCEPTED SOLUTION

JamesD
Tera Contributor

Hi Jim,



I don't think you can declare 'one' in the same operation you are incrementing 'sDt'.



Have you tried just declaring both at the beginning, then increment 'one' by itself:



var sDt = new GlideDateTime(current.start_date);


var one = new GlideDateTime(current.start_date);


one.addDaysLocalTime(-1);



Then the other operations after that, it might work for you.



*Just tried a quick script to play with the date manipulations above, that at least seemed to work on my end. 🙂


View solution in original post

2 REPLIES 2

JamesD
Tera Contributor

Hi Jim,



I don't think you can declare 'one' in the same operation you are incrementing 'sDt'.



Have you tried just declaring both at the beginning, then increment 'one' by itself:



var sDt = new GlideDateTime(current.start_date);


var one = new GlideDateTime(current.start_date);


one.addDaysLocalTime(-1);



Then the other operations after that, it might work for you.



*Just tried a quick script to play with the date manipulations above, that at least seemed to work on my end. 🙂


Success!   thank you so much, I don't have anyone to bounce this stuff off of, this is why I love the community so much.  



I really appreciate the help!