Background script for updating date / time on fields

ghouse
Tera Contributor

HI service now team,

                   I am trying to Update date /time fields value on a closed incident through background script , but iam unable to update them through the background script i have written . but iam unable to acheive any results , can you correct my script if there is anything wrong or suggest me good scripting for updating the fields. 

iam trying to update this fields 

 Restore date (empty field value to )==>Mar-18-2022 10:10:00 PM,

 Actual End date (existing old value)==> Mar-18-2022 11:05:00 PM

var work_end = "03-18-2022 10:10:00";
var u_user_services_restored_date_ = ' ';
var gr= new GlideRecord('incident');
gr.addEncodedQuery('u_major_incident=true^incident_state=7^number=INC3486819');
//gr.setLimit(2);
gr.query();
while(gr.next())
{
gr.work_end = gr.getDisplayValue();
gr.setValue("work_end",'03-18-2022 11:05:00');
gr.setValue("u_user_services_restored_date_",'03-18-2022 10:10:00');
gr.setWorkflow(false);
gr.autoSysfields(false);
gr.Update();
}

Can any one of you could help me in this and provide me a good solution. thanks .

Regards,

Ghouse sharief

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this for 2 records and verify and then comment setLimit() line of code

var work_end = "2022-03-18 23:05:00";
var u_user_services_restored_date_ = ' ';
var gr = new GlideRecord('incident');
gr.addEncodedQuery('u_major_incident=true^incident_state=7');
gr.setLimit(2);
gr.query();
while(gr.next())
{
	gr.work_end = new GlideDateTime(work_end);
	gr.setValue("work_end", new GlideDateTime(work_end));
	gr.setValue("u_user_services_restored_date_", new GlideDateTime('2022-03-18 22:10:00'));
	gr.setWorkflow(false);
	gr.autoSysfields(false);
	gr.update();
}

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Michael de Boer
Giga Guru

I think date value format is: yyyy-mm-dd hh:MM:ss
Can you try that format in you script?

And do you get an error, when running it as a background script?

Regards,

Michael

Regards,
Michael

Please mark the suggestion as helpful/like, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided has resolved your query.

Hi michael , 

           I tried it but it is not working and not displaying any results . No iam not getting any error.

Thanks .

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this for 2 records and verify and then comment setLimit() line of code

var work_end = "2022-03-18 23:05:00";
var u_user_services_restored_date_ = ' ';
var gr = new GlideRecord('incident');
gr.addEncodedQuery('u_major_incident=true^incident_state=7');
gr.setLimit(2);
gr.query();
while(gr.next())
{
	gr.work_end = new GlideDateTime(work_end);
	gr.setValue("work_end", new GlideDateTime(work_end));
	gr.setValue("u_user_services_restored_date_", new GlideDateTime('2022-03-18 22:10:00'));
	gr.setWorkflow(false);
	gr.autoSysfields(false);
	gr.update();
}

Regards
Ankur

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

Hi Ankur,

     That actually worked , thanks for your help .