Cannot figure out why form field does not update at end of script

Digit
Kilo Guru

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();
}
1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

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

View solution in original post

5 REPLIES 5

OlaN
Giga Sage
Giga Sage

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

vkachineni
Kilo Sage
Kilo Sage

var startDate = new GlideDateTime('arch.u_assigned_state');

should be

var startDate = new GlideDateTime(arch.u_assigned_state); //no quotes

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

OlaN
Giga Sage
Giga Sage

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?

Digit
Kilo Guru

OlaN,
Dropping the 'arch.' in the setValue was the culprit.

I owe you a beer. Thanks so much for the help!!