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

Inactive_Us1474
Giga Guru

You can refer below :

(function(){

var rec = new GlideRecord('incident');
rec.addQuery('state', '>','3'); // You can refine your incident query more.
rec.query();

while (rec.next()){

var gr = new GlideRecord("task_sla");
gr.addQuery('task',rec.sys_id);
gr.addQuery('sla','sla_sys_id to match');
gr.query();
if(gr.next())
{
rec.u_gdt_p1 = gr.start_time;
rec.update();
}
}
})();

Hi Akhil,

 

Thanks for the response, I have tried the below, targeting all P1 incidents that are closed in the instance and it hasn't updated the field 

(function(){

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

while (rec.next()){
	var gr = new GlideRecord("task_sla");
	gr.addQuery('task',rec.sys_id);
	gr.addQuery('sla','sla_sys_id to match');
	gr.query();
	if(gr.next()){
		
		rec.u_gdt_p1 = gr.start_time;
		rec.update();
	}
}
})();

Can you offer any more assistance?

Could you please run the below code in the back ground script and share the output.

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','sys_id of P1 sla to match');
gr.query();
gs.print("SLA count: " +gr.getRowCount());
}

*** Script: Incident: 29493
*** Script: SLA count: 0