- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 07:08 AM
I have been pulling my hair out trying to figure why my scheduled job script is not updating the actual form field. I am calculating the duration a form is in a state on a daily basis, and adding the new value to the existing value (running tally).
The script functions as expected up until the line where I am trying to push the aggregated value to the form field (u_time_in_assigned), which is an integer type. I've checked formatting (using parseInt and math.round) with no luck, as well as setValue(), and just doing an x=y.
The final log statement displays the expected value, but when I look at the actual form, the value does not get updated.
Really appreciate any assistance with this!!
var arch = new GlideRecord('u_architecture_request');
arch.addEncodedQuery('active=true^sys_class_name=u_architecture_request^stateIN22');
arch.query();
while (arch.next()) {
//calculate time since last run and add to duration
var initTime = arch.u_time_in_assigned;
gs.log('Number: ' + arch.number.getDisplayValue() + '\nOld time in assigned: ' + initTime + '\nu_assigned_state: ' + arch.u_assigned_state, 'test');
// Calculate the new duration based on the 24 hour weekdays excluding holidays schedule
var startDate = new GlideDateTime('arch.u_assigned_state');
var endDate = new GlideDateTime();
var schedule = new GlideSchedule('563653c8db670bc09b65v53a8c96194d', 'UTC'); // loads 24 hour weekdays excluding holidays schedule
arch.u_assigned_duration = schedule.duration(startDate, endDate);
//update the aggregated duration after converting from milliseconds to minutes
var u_time = (arch.u_assigned_duration.dateNumericValue() / (60 * 1000)) + initTime;
var uTime2 = parseInt(u_time);
arch.setValue(arch.u_time_in_assigned, uTime2);
gs.log('Number: ' + arch.number.getDisplayValue() + '\nNew time in assigned: ' + arch.u_time_in_assigned, 'test');
arch.update();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 07:14 AM
Hi,
I believe this is the error
// replace this
arch.setValue(arch.u_time_in_assigned, uTime2);
// with this
arch.setValue('u_time_in_assigned', uTime2);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 07:14 AM
Hi,
I believe this is the error
// replace this
arch.setValue(arch.u_time_in_assigned, uTime2);
// with this
arch.setValue('u_time_in_assigned', uTime2);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 07:15 AM
var startDate = new GlideDateTime('arch.u_assigned_state');
should be
var startDate = new GlideDateTime(arch.u_assigned_state); //no quotes
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 07:17 AM
Some additional pointers.
Use getValue() method whenever possible.
Some examples:
var initTime = arch.u_time_in_assigned;
// should be
var initTime = arch.getValue('u_time_in_assigned');
var startDate = new GlideDateTime('arch.u_assigned_state');
// will not work, should be
var startDate = new GlideDateTime(arch.getValue('u_assigned_state')); // is a field called "state" really a datetime value?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 08:03 AM
OlaN,
Dropping the 'arch.' in the setValue was the culprit.
I owe you a beer. Thanks so much for the help!!