The CreatorCon Call for Content is officially open! Get started here.

How to set Duration field using back ground script?

prasannakumard
Tera Guru

Hi Team,

I need to set below value in glide_duration type field. Please let me know how to set this value into Duration field:

Time = 100 (minutes)

How to set this 100 mins into Duration field.

I tried like below, but, the vaqlue is not set into duration field.

current.time_worked = new GlideDuration('100');

Please suggest and provide proper way/script to set minutes value into Duration field.  

Thanks & Regards,

Prasanna Kumar

1 ACCEPTED SOLUTION

the following worked for me, I created a new field on my table with type 'glide_time'.

 

var xTable = new GlideRecord('x_snc_bctestappnew_app_table');
xTable.query();
while (xTable.next()) {
    gs.info("u_time = " + xTable.u_time + ".");
}
//new record
var numMinutes = 100;
var minutes = numMinutes%60;
var hours = (numMinutes - minutes)/60;
var timeVal = "1970-01-01 " + hours.toString() + ":" + minutes.toString() + ":00";
gs.info("timeVal = " + timeVal + ".");
xTable.initialize();
xTable.u_time = timeVal;
xTable.u_name = "New record";
xTable.insert();

you need extra logic if your value of minutes that you start with is greater than 1440 (one day).

View solution in original post

9 REPLIES 9

And duration fields are the number of seconds from 1970-01-01 00:00:00.

 

Hi @Bert_c1 ,

After getting hours and minutes, how to set these values to glide_time field?

var numMinutes = time; (here time=100)
var minutes = numMinutes%60;gs.print("minutes: "+minutes);
var hours = (numMinutes - minutes)/60; gs.print("hours: "+hours);

In Incident the duration field is "u_aws_time_worked"

So,

var fin = new GlideDuration('0 '+hours+':'+minutes+':00');
inc.u_aws_time_worked = fin;
inc.update(); 

 

Out Put: 

*** Script: minutes: 40
*** Script: hours: 1
*** Script: numMinutes = 100 = 1 hours and 40 minutes.
*** Script: 1970-01-01 01:40:00

But, nothing updated in that field. Showing like below:

 

prasannakumard_0-1669628433897.png

prasannakumard_1-1669629033553.png

 

Please let me know how to update the resulting converting time into duration field.

 

Thanks you so much for your help in advance.

 

Thanks & Regards,

Prasanna Kumar

 

The field type should be "glide_duration" and not "glide_time"

var durStart = new GlideDateTime(gs.nowDateTime());
var durEnd = new GlideDateTime(gs.nowDateTime());
gs.print(durStart);
durEnd.addSeconds(6000); //From your calc minutes expressed as seconds 30 min * 60 sec = 1800 sec
gs.print(durEnd);

var duration = GlideDateTime.subtract(durStart, durEnd);
gs.print(duration.getDisplayValue());
var recGR = new GlideRecord("incident");
if(recGR.get("56c13d722fd230104fce5d492799b676")){
recGR.u_aws_time_duration = duration;
recGR.update();
}

 

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

Hi Vinod,

Actually, it is showing Glide_time only. It's Time type field. not Duration type field. 

prasannakumard_0-1669643142650.pngprasannakumard_1-1669643151227.png

So, I can't able to set the value to that field. 

 

Thanks & Regards,

Prasanna Kumar

the following worked for me, I created a new field on my table with type 'glide_time'.

 

var xTable = new GlideRecord('x_snc_bctestappnew_app_table');
xTable.query();
while (xTable.next()) {
    gs.info("u_time = " + xTable.u_time + ".");
}
//new record
var numMinutes = 100;
var minutes = numMinutes%60;
var hours = (numMinutes - minutes)/60;
var timeVal = "1970-01-01 " + hours.toString() + ":" + minutes.toString() + ":00";
gs.info("timeVal = " + timeVal + ".");
xTable.initialize();
xTable.u_time = timeVal;
xTable.u_name = "New record";
xTable.insert();

you need extra logic if your value of minutes that you start with is greater than 1440 (one day).