setting value in a duration type field

Deepika54
Tera Contributor

Hello experts,

 

I am getting a value in seconds. example 43,200 seconds which is 12 hours.

or 86400 seconds which is 1 day.

 

I need to update 1 day or 12 hours ,i.e these type of values in the duration field of the sla definition table. Kindly help.

Deepika54_0-1752076414640.png

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

ServiceNow's duration fields technically store values in milliseconds behind the scenes, but the GlideDuration object handles the conversion for you when you provide it the correct input.

// Let's say your number of seconds is in a variable called 'secondsValue'
var secondsValue = 43200; // Example: 12 hours

// Convert seconds to milliseconds
var milliseconds = secondsValue * 1000;

// Create a new GlideDuration object using the milliseconds
var duration = new GlideDuration(milliseconds);

// Now, update your SLA Definition record
var slaGr = new GlideRecord('contract_sla'); // Or 'sn_sla_definition' if you're using the new table name
slaGr.get('sys_id_of_your_sla_definition'); // Replace with the actual sys_id of your SLA Definition
slaGr.duration = duration; // Set the duration field
slaGr.update();

gs.info("Updated SLA duration to: " + duration.getDisplayValue());

 

View solution in original post

5 REPLIES 5

Shubham_Jain
Mega Sage

@Deepika54  Give try using this:

 

var slaGr = new GlideRecord('contract_sla');
if (slaGr.get('sys_id', 'PUT_YOUR_SLA_SYS_ID_HERE')) {
    var seconds = 43200; // Ex.: 12 hours
    var duration = new GlideDuration();
    duration.setValue(seconds * 1000); // GlideDuration expects milliseconds
    slaGr.setValue('duration', duration);
    slaGr.update();
}

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain