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

My mistake, that is what I meant-to have it closed 24 hours before.  For some reason, when our instance was set up, the resolved_by and resolved_at were not filled in, so we have a lot of records where this is the case, and we wanted to fill those in.  

Well there you go. If your question is answered please do mark an answer correct to close the thread down. Marking answers helpful where you deem it to be necessary is also appreciated 🙂

Thanks!  Got another one for you-

recUser.resolved_by = ''; // Pass the sys_id of the user

Can I use a reference value here? as in resolved_by=closed_by, or do I have to use a specific user in the field?

Try if this works:

 

recUser.resolved_by = g_form.getValue('closed_by');

Hi Heather,

Bit late on this one but nonetheless, you can set the resolved by to the closed by like so:

recUser.resolved_by = recUser.closed_by;