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

Ankur Bawiskar
Tera Patron
Tera Patron

@Deepika54 

to set duration field you can use setDateNumericValue() method

pass milliseconds to it

something like this

gr.u_field.setDateNumericValue(86400000); // pass milliseconds

gr.update();

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Deepika54 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Deepika54 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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