Background Script to update Date Fields

Community Alums
Not applicable

Hi all,

 

I have to update some records in Change table i.e i have to set work start with created date , work end with created +30 seconds .

I have written like below but not working

Can you please help

var gr= new GlideRecord('change_request');
gr.addEncodedQuery('short_descriptionLIKEISPW-change^active=true^sys_created_by=ispw');
gr.setLimit(2);
gr.query();
while(gr.next())
{
gr.work_start = gr.sys_created_on;
gr.requested_by_date = gr.sys_created_on;
var date = new GlideDateTime('sys_created_on');
gr.work_end = date.addSeconds(10);
gr.update();
}

1 ACCEPTED SOLUTION

VigneshMC
Mega Sage

Try

var gr= new GlideRecord('change_request');
gr.addEncodedQuery('short_descriptionLIKEISPW-change^active=true^sys_created_by=ispw');
gr.setLimit(2);
gr.query();
while(gr.next())
{
gr.work_start = gr.sys_created_on;
gr.requested_by_date = gr.sys_created_on;
var date = new GlideDateTime(gr.sys_created_on);
date.addSeconds(30);
gr.setValue('work_end', date.getValue());
//gs.print(date.getValue());
gr.update();
}

View solution in original post

17 REPLIES 17

Community Alums
Not applicable

no its not working

Quick question: are you needing all three values to be the 'updated' sys_created_on value? Or is it that the work_start and requested_by_date are both to be 10secs behind the new date value?

 

I went back and checked on your original post where you said "i have to set work start with created date , work end with created +30 seconds". So here is a slightly modified version of my script excerpt which should do just that:

{
	var gdt = new GlideDateTime(current.sys_created_on);
	current.work_start = gdt.getValue();
	var date = new GlideDateTime(gdt);
	date.addSeconds(30);
	current.work_end = date.getValue();
}

However, a lot also comes down to your actual query being correct otherwise the script will never run.