add seconds and convert to date

radt
Kilo Guru

I have one duration field so I converted it to seconds using attribute  set to seconds. This value is milliseconds and so to get value I used:  (time_left is the duration field value)
var Duration = (current.time_left.getGlideObject().getNumericValue() / 1000); // multiple by .001 or divide by 1000


Now In duration I have days, hours, minutes in complete seconds .

 

 

So I have date field now which needs to be converted to seconds again: on_hold_till is the date field value

I have this code: 


var gdt = new GlideDateTime();
gdt.setValue(current.getValue('on_hold_till'));
var ms = gdt.getNumerciValue();

Now in var Duration I have duration in seconds and now in Var ms I have date field in seconds.

 

So now I have to add this and set this to a new field. ( I want duration to add to a date field to get a new date field):

like adding both this seconds and then converting totals seconds to a date field.

 

Any help on this!!!!! Please 

If you have easy way to add duration to date feel free to provide using the above field values.

 

TIA

 

1 ACCEPTED SOLUTION

Hi,

do this

// to get duration time to seconds.

var startTime = new GlideDateTime(current.u_date.getDisplayValue());
var duration = current.time_left.dateNumericValue();
startTime.add(duration);
current.u_target_date = startTime.getDate();

// OR if u_target_date is of type date/time then do this

current.u_target_date = startTime.getDisplayValue();

current.update();

Regards
Ankur

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

View solution in original post

12 REPLIES 12

Not working... That is SLA duration. Getting Nan.. Is there anything from our end configured with SLA? Duration in case field works fine.

 

 

I am using this code in PDI and it worked fine with BR but when used with my office instance the nothing works.

 

var seconds = (current.u_remaining_hours.getGlideObject().getNumericValue()/1000);

gs.addInfoMessage('duration seconds is' + seconds);
gs.info(" seconds ");



// to change date field to seconds
var gdt = new GlideDateTime();
gdt.setValue(current.getValue('u_on_hold_till'));
var ms = gdt.getNumericValue()/1000;
gs.addInfoMessage('seconds is added as' + ms );
gs.info(" ms ");


// to get duration time to seconds.

// var seconds = (current.time_left.getGlideObject().getNumericValue()/1000);

// gs.addInfoMessage('duration seconds is' + seconds);


// add both seconds field.
var value = seconds + ms;

gs.addInfoMessage('total time' + value);
gs.info(" value ");

// convert total seconds to date field.
var dt = new GlideDateTime("1970-01-01 00:00:00");
dt.addSeconds(value);
gs.info(dt.getValue());
//dt.addSeconds(value);
current.u_target_date_sla = dt;
current.u_target_date = dt;
gs.info(" dt ");
current.update();

 

I used GlideObject to convert Duration to seconds. So getting error: 

GlideObject can't be used in Scope application.

 

u_remaining_hours is duration field which should be converted to seconds.

 

u_on_hold_till is date field which should be converted to seconds.

 

then add both the seconds and convert total seconds to date ..

this date should be set to u_target_date.

 

Can you please replace Glideobject. I tried the above code you provided but not sure whether I messed it up but it didnt work.

 

Anything wrong with my code? Why is it not working in my scoped app but worked fine in personal instance.

I have asked many question on this single functionality and i thought finally this worked but 😞 I don't think soo yet .

TIA

@radt 

I have shared the script without getGlideObject()

yes getGlideObject won't work in scoped application

The script I shared will work fine in scoped and global application both

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

 

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

You are always a savior 🙂  I tried to write BR in Global and used this for HR CASE and this worked. I was creating BR in scoped app so didnt work. Your code helped me to complete which I started. This is a big relief for me now ... I literally asked 3,4 questions on this.. 

 

Thank you soo muchhhhhhhhh!!!!!!!!!!!!!!!!

 

Now I am down to 1 functionality out of 9.

 

You can see the below image Rejection SLA 16. 

There is one SLA getting added if case is rejected. That sla is 16 hours. So this is working fine.

But target date is not getting updated with this 16 hours after rejected.

 

I used : 


{
var gr = new GlideRecord("sn_hr_core_case");
gr.addQuery("sys_id", current.task);
gr.query();
if (gr.next()) {
gr.u_target_date = current.planned_end_time;
gr.u_target_date_sla = current.planned_end_time;
//gr.u_sla_breached_date = current.planned_end_time;
gr.update();
gs.info( "Sample message");

}

 

but some how my code is not working. It worked in PDI but in office instance not working. I see this might be issue with conditions but not sure how to figure this.

Or any BR is over riding 😞

conditions is: 

 

task.type is xxxx

 we have 4 coe's so added 4 task types and 

added 

sla definition is xxx ( this is the rejected sla that is getting added once case is rejected).

 

This is a simple function but eating my head,...

Hi,

Did the BR run?

Is the field name correct?

Since your BR is in global scope, possibly write access to field on HR table is not allowed.

Please check any record got created in application restricted caller access for the field

Regards
Ankur

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