Background Script Help to Update Task based on Parent

Hari7
Kilo Guru

I wanted to update the incident task callerid field for the tasks which are empty currently and copy the value from incident callerid to incident task caller id.

 

This is the script I have but it is getting me into loops

 

var grchange = new GlideRecord('incident_task');
grchange.addEncodedQuery("calleridISEMPTY");
grchange.setLimit(15);
grchange.query();
if(grchange.next()){
grchange.account = grchange.parent.account;
grchange.setWorkflow(false);
grchange.updateMultiple();
}

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

The Incident Task table does not have a calledrid field out of the box.  If you have added one in your instance, ensure the Column name is exactly 'callerid', which you use in the encoded query, yet you are setting a field named 'account' to that of the parent? That seems like it should be grchange.callerid = grchange.incident.caller_id; depending on the field name for incident_task.  The incident field is populated more reliably/consistently than the parent field, unless you have a Business Rule in place to populate parent.  ServiceNow has actually started recommending against the use of updateMultiple().  Instead, change the if(grchange.next()){ to when(grchange.next()){ then grchange.update();

 

var grchange = new GlideRecord('incident_task');
grchange.addEncodedQuery("calleridISEMPTY"); //verify a field named callerid exists on your incident_task table
grchange.setLimit(15);
grchange.query();
while (grchange.next()) {
    grchange.callerid = grchange.incident.caller_id;
    grchange.setWorkflow(false);
    grchange.update();
}

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

The Incident Task table does not have a calledrid field out of the box.  If you have added one in your instance, ensure the Column name is exactly 'callerid', which you use in the encoded query, yet you are setting a field named 'account' to that of the parent? That seems like it should be grchange.callerid = grchange.incident.caller_id; depending on the field name for incident_task.  The incident field is populated more reliably/consistently than the parent field, unless you have a Business Rule in place to populate parent.  ServiceNow has actually started recommending against the use of updateMultiple().  Instead, change the if(grchange.next()){ to when(grchange.next()){ then grchange.update();

 

var grchange = new GlideRecord('incident_task');
grchange.addEncodedQuery("calleridISEMPTY"); //verify a field named callerid exists on your incident_task table
grchange.setLimit(15);
grchange.query();
while (grchange.next()) {
    grchange.callerid = grchange.incident.caller_id;
    grchange.setWorkflow(false);
    grchange.update();
}

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Hari7 ,

 

Can u try this code

 

// Query incident tasks where caller_id is empty

var grChange = new GlideRecord('incident_task');

grChange.addNullQuery('caller_id');

grChange.query();

 

while (grChange.next()) {

        // Copy the caller_id from the parent incident to the incident task

        grChange.caller_id = grChange.parent.caller_id;

        grChange.setWorkflow(false);

        grChange.autoSysFields(false); // To avoid updating system fields

        grChange.update(); 

}

 

Thanks,

Danish