Query and Update using a script

Heather White
Mega Guru

 

Hi Community,

I would like to create a  script that will query any records where resolved_at and resolved_by are empty, and set them to a specific value. 

I had assistance creating the following script to update records on my user table.

How do I alter the script to work on the incident table?

var recUser = new GlideRecord('sys_user');
recUser.addEncodedQuery('company=a828cffe993928006d095f1cc569621f^sys_class_name!=sys_user^ORsys_class_name=NULL');
recUser.query();
gs.info(recUser.getRowCount());
while(recUser.next()){
recUser.sys_class_name = 'sys_user';
recUser.update();
}

Thanks,

Heather

 

1 ACCEPTED SOLUTION

You want the resolved_at to be 24 hours after the incident was closed? Wouldn't that be a bit weird? To have the ticket resolved after it was closed?

You can do it this way but check out how to do it 24 hours before as well:

//to set resolved after closed:
var gdt = new GlideDateTime(recUser.getValue('closed_at'));
gdt.addDays(1);
recUser.resolved_at = gdt;
recUser.resolved_by = ''; // Pass the sys_id of the user
recUser.update();

//to set resolved before closed:
var gdt = new GlideDateTime(recUser.getValue('closed_at'));
gdt.addDays(-1);
recUser.resolved_at = gdt;
recUser.resolved_by = ''; // Pass the sys_id of the user
recUser.update();

View solution in original post

10 REPLIES 10

Prateek kumar
Mega Sage

Try this:

var recUser = new GlideRecord('incident');
recUser.addEncodedQuery('resolved_atISEMPTY^resolved_byISEMPTY');
recUser.query();
gs.info(recUser.getRowCount());
while(recUser.next()){
recUser.YourVariableName= 'YourValue'; // Change Accordingly

//recUser.update();  //Before updating print the number of records and confirm
}


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

dvp
Mega Sage
Mega Sage

Try the below script. Update the values as needed.

Also, I would encourage you to first test with single record and then apply to all records with empty resolved at and resolved by

var recUser = new GlideRecord('incident');
recUser.addEncodedQuery('resolved_atISEMPTY^resolved_byISEMPTY');
recUser.query();
gs.info(recUser.getRowCount());
while(recUser.next()){
	recUser.resolved_at = gs.nowDateTime(); //Current date time
	recUser.resolved_by = ''; // Pass the sys_id of the user
	recUser.update();
}

Thanks, this worked well!  Follow up question-how would I change the resolved_at time to be 24 hours after the closed date? 

You want the resolved_at to be 24 hours after the incident was closed? Wouldn't that be a bit weird? To have the ticket resolved after it was closed?

You can do it this way but check out how to do it 24 hours before as well:

//to set resolved after closed:
var gdt = new GlideDateTime(recUser.getValue('closed_at'));
gdt.addDays(1);
recUser.resolved_at = gdt;
recUser.resolved_by = ''; // Pass the sys_id of the user
recUser.update();

//to set resolved before closed:
var gdt = new GlideDateTime(recUser.getValue('closed_at'));
gdt.addDays(-1);
recUser.resolved_at = gdt;
recUser.resolved_by = ''; // Pass the sys_id of the user
recUser.update();