Background script to update custom field with Task SLA time

caliban
Tera Expert

Hi there,

 

I've created a custom field and a business rule to add the time a P1 incident was started on the Incident form.  This BR works fine 

Before on Insert/Update

When to Run: SLA Definition is P1

(function executeRule(current, previous /*null when async*/) {
	
	var inc = new GlideRecord('incident');
	inc.addQuery('sys_id',current.task);
	inc.query();
	if(inc.next()){
		inc.u_gdt_p1 = current.start_time.getDisplayValue();
		inc.update();
	}
	
})(current, previous);

Now I want to run a background script to update all of the old incidents but I am unsure how to reference the start_time from the Task SLA related list as I want to make sure it is only the P1 SLA time that gets inserted into the field.  I'm guessing it is something like the below, but if anyone can help me fill in the blank(s) I'd be most grateful.

Background script:

(function(){

var rec = new GlideRecord('incident');
rec.addQuery('state', >3);
rec.query();

while (rec.next()){
	rec.u_gdt_p1 = [INSERT P1 TASK SLA 'start_time'];
	rec.update();
}
})();
1 ACCEPTED SOLUTION

You need to make sure that the data is present on the task_sla for the incidents in order to update.

var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.addQuery('priority', '<', 3);
rec.query();

gs.print(rec.getRowCount());

while (rec.next()){
var gr = new GlideRecord("task_sla");
gr.addQuery('task',rec.sys_id);
gr.addQuery('sla','sla_sys_id to match'); // sys_id of the sla
gr.query();
if(gr.next()){
gs.print(gr.start_time); // if it prints then there is data present else not
rec.u_gdt_p1 = gr.start_time;
rec.update();
}
}

You can cross check via list view.

View solution in original post

9 REPLIES 9

Have you replaced sys_id of P1 sla to match with your p1 sla sys_id.

var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.addQuery('priority', '<', 3);
rec.query();
gs.print("Incident: " +rec.getRowCount());

if(rec.next())
{
var gr = new GlideRecord("task_sla");
gr.addQuery('task',rec.sys_id);
gr.addQuery('sla','4b8ec7020a0a0b30007c21d33262147f');
gr.query();
gs.print("SLA count: " +gr.getRowCount());
}

Yes, I'm taking the sys_id from the SLA Definitions table.  Right Click, copy sys_id.  Is that correct?

Thank you for quick response.

Yes it is correct. Could you filter the same query in the list view of task_sla form.

1.Copy the incident number which is not active and priority is critical.

2.Navigate to task_sla.list

3. Filter the list view as sla definition is your p1 SLA and incident is copied incident in step 1.

Let me know the result.

-Tushar 

You need to make sure that the data is present on the task_sla for the incidents in order to update.

var rec = new GlideRecord('incident');
rec.addQuery('active',false);
rec.addQuery('priority', '<', 3);
rec.query();

gs.print(rec.getRowCount());

while (rec.next()){
var gr = new GlideRecord("task_sla");
gr.addQuery('task',rec.sys_id);
gr.addQuery('sla','sla_sys_id to match'); // sys_id of the sla
gr.query();
if(gr.next()){
gs.print(gr.start_time); // if it prints then there is data present else not
rec.u_gdt_p1 = gr.start_time;
rec.update();
}
}

You can cross check via list view.

Thank you both for your assistance, Akhil your script did work fine with some pointers from Tushar on the task_sla list I ended up get the script to complete with an encoded query.

Tushar, I'm still in the dark as to why your search didn't work via the script but I was able to find the records manually from the task_sla list