I'm failry new to Javascript and Servicenow so bear with me please. I'm creating a fix script to populate the incident breach time label on the incident task. It will show the same SLA as the parent incident. My below script returns the correct number of records that should be updated. However, when I go to update the record in my second GlideRecord, it only updates one record(Should be about 125). I commented the update portion of the code for testing purposes. Any suggestions? Thanks
var grSLA = new GlideRecord('u_incident_task');
grSLA.addEncodedQuery('u_incident_breach_timeISEMPTY^parentISNOTEMPTY^stateIN1,2,4');
grSLA.query();
var count=0;
gs.info("{0} of Itasks found to update", grSLA.getRowCount());
while (grSLA.next())
{
var ipar = grSLA.parent.sys_id;
}
gs.print(ipar);
var grName = new GlideRecord('task_sla');
grName.addQuery('task', ipar);
grName.addQuery('stage', 'in_progress');
grName.query();
while (grName.next())
{
var bTime = grName.planned_end_time.getDisplayValue();
grSLA.u_incident_breach_time=bTime; //sets the Incident Breach Time on the iTask
//grSLA.update();
count++;
}
I believe you need to move your task_sla
loop inside of your u_incident_task
loop.
It is only updating the final one since ipar
is the last record in the loop.
var grSLA = new GlideRecord('u_incident_task');
grSLA.addEncodedQuery('u_incident_breach_timeISEMPTY^parentISNOTEMPTY^stateIN1,2,4');
grSLA.query();
var count=0;
gs.info("{0} of Itasks found to update", grSLA.getRowCount());
while (grSLA.next())
{
var ipar = grSLA.parent.sys_id;
gs.print(ipar);
var grName = new GlideRecord('task_sla');
grName.addQuery('task', ipar);
grName.addQuery('stage', 'in_progress');
grName.query();
while (grName.next())
{
var bTime = grName.planned_end_time.getDisplayValue();
grSLA.u_incident_breach_time=bTime; //sets the Incident Breach Time on the iTask
grSLA.update();
count++;
}
}