Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update Due Date on Incident Table based on Breach Time from Task SLA Table using Background/Fix Script

iDNS
Tera Expert

Hi,

I am trying to update 'Due Date' on the Incident table for older Incidents based on the 'Breach Time' from the Task SLA (task_sla) table via a Background/Fix Script.

Business Rule doesn't help as these Incidents do not get updated & will be an one time update. Due Date is currently incorrect or empty on Incident Table & was trying via a Background/Fix Script using a Encoded Query on both the tables. 

The below script works but updates only 1 record where as I would want to update all records that are in the Incident's Encoded Query. 

I am using the below but haven't had much success. Basically this is something like a INNER JOIN in SQL & Update TableA.Column from TableB.Column based on an INNER JOIN where TableA's task.number = TableB's number. 

Anything you would point me to will be a great help. Thanks

var gr = new GlideRecord('task_sla');
gr.addEncodedQuery("task.numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
gr.query();
while(gr.next()){
  var vend = new GlideRecord('incident');
  vend.addEncodedQuery("numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
  vend.query();
  if(vend.next()){
        vend.due_date = gr.planned_end_time;
        vend.update();
}
gs.print(vend.number +' ' + gr.planned_end_time);
}
1 ACCEPTED SOLUTION

@iDNS - Ah I got you.

try this.

var gr = new GlideRecord('task_sla');
gr.addEncodedQuery("task.numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
gr.query();
while(gr.next()){
  var vend = new GlideRecord('incident');
  vend.addEncodedQuery("number="+gr.task.number);
  vend.query();
  if(vend.next()){
        vend.due_date = gr.planned_end_time;
        vend.update();
}
gs.print(vend.number +' ' + gr.planned_end_time);
}
Regards,
Muhammad

View solution in original post

4 REPLIES 4

MrMuhammad
Giga Sage

Hi @iDNS,

Please try this. In the second GlideRecord I replaced the if with while to have it iterate through all the records instead of just the first one.

var gr = new GlideRecord('task_sla');
gr.addEncodedQuery("task.numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
gr.query();
while(gr.next()){
  var vend = new GlideRecord('incident');
  vend.addEncodedQuery("numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
  vend.query();
  while(vend.next()){
        vend.due_date = gr.planned_end_time;
        vend.update();
}
gs.print(vend.number +' ' + gr.planned_end_time);
}

Please mark this helpful/correct, if applicable.

Regards,

Muhammad

 

Regards,
Muhammad

Thanks Muhammad,

This updated the value from first task_sla's planned_end_time column into all other Incidents due_date column. 

@iDNS - Ah I got you.

try this.

var gr = new GlideRecord('task_sla');
gr.addEncodedQuery("task.numberININC0000046,INC0000050,INC0000051,INC0000052,INC0000053,INC0000054");
gr.query();
while(gr.next()){
  var vend = new GlideRecord('incident');
  vend.addEncodedQuery("number="+gr.task.number);
  vend.query();
  if(vend.next()){
        vend.due_date = gr.planned_end_time;
        vend.update();
}
gs.print(vend.number +' ' + gr.planned_end_time);
}
Regards,
Muhammad

iDNS
Tera Expert

Thank You, that worked 🙂