- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 03:57 AM
I have a requirement I'm working on but don't get it to work.
I'm running a scheduled script to see if 3 working days has past since a known date and time. If more then 3 days (24h of work time) has passed I should set a checkbox to true.
The schedule I'm using is from 8am to 4pm each day, total of 8h.
This is what I've been trying but there is something wrong with the duration calculation, I also need help on how to see if more then 24h of work time has passed.
I never see "gs.log('DURATION: ' + dur);" in the sys log, but I see the number in the log.
var test = new GlideRecord('u_rekrytering');
test.addQuery('number', 'REKR0000006');
test.query();
while(test.next()){
gs.log('---THIS IS LOG FOR ' + test.number + '---');
var schedule = new GlideSchedule('4478d9d969bfa100f336d9fc410262ec');
var dur = schedule.duration(test.u_start, gs.nowDateTime());
gs.log('DURATION: ' + dur);
if (dur >= ---More than 24h work hours---) {
test.u_awaiting_assignee = 'true';
test.update();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2016 02:11 AM
I've got it to work now, instead of the previous solution I'm using DurationCalculator() which seems to be the easiest way to calculate the duration based on a schedule.
var ser = new GlideRecord('u_rekrytering');
ser.addQuery('number', 'REKR0000070');
ser.query();
while(ser.next()){
var dc = new DurationCalculator();
dc.setSchedule('4478d9d969bfa100f336d9fc410262ec');
var dur = dc.calcScheduleDuration(ser.u_planned_end_time, gs.nowDateTime());
if (dur > 86400) {
//86400 sec = 24h = 3 days according to schedule
ser.u_awaiting_assignee = 'true';
ser.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2016 11:10 AM
Hmm your right,
dateDiff don't care about schedule.
I've been pulling my hair on this one. It is really annoying. No matter how I put gs.nowDateTime(), i get an error.
as long as I use 2 fields from the object it work, but use a variable from the script and it stops..
Now, I got it to work, but I aint proud of the solution. But atleast it wouldn't be a showstopper if it is.
What I do is that I borrow a field on the record in the script
I did this in a before BR on change but I think you understand it:
(function executeRule(current, previous /*null when async*/) {
var temp = current.start_date; //Save the data on so I can use this field
current.start_date = gs.nowDateTime();//Now set the field to have current datetime
var sched = new GlideSchedule('7cf1bb310fb01e004cf365ba32050ec6');
var dur = sched.duration(current.start_date.getGlideObject(), current.end_date.getGlideObject())
current.start_date = temp;//Put back to correct info
var durNum = dur.getNumericValue();
var durationSeconds = (durNum/1000);
if (durationSeconds > 86400) { //86400 sec = 24h = 3 days
gs.addErrorMessage("more Than 3 days");//Just to show it works
}
})(current, previous);
//Göran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2016 02:11 AM
I've got it to work now, instead of the previous solution I'm using DurationCalculator() which seems to be the easiest way to calculate the duration based on a schedule.
var ser = new GlideRecord('u_rekrytering');
ser.addQuery('number', 'REKR0000070');
ser.query();
while(ser.next()){
var dc = new DurationCalculator();
dc.setSchedule('4478d9d969bfa100f336d9fc410262ec');
var dur = dc.calcScheduleDuration(ser.u_planned_end_time, gs.nowDateTime());
if (dur > 86400) {
//86400 sec = 24h = 3 days according to schedule
ser.u_awaiting_assignee = 'true';
ser.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2016 02:30 AM
Ahh nice 😃
I forwarded your problem to my ServiceNow teacher I had in scripting and his first response that it all looked good and he couldn't understand why it didn't work.. But he should look into it some more.. I'll come back if he gets any explanation