Trying to update a field of closed incidents through a script

Sunny Joel
Giga Expert

wrote a script under "sysauto_script.list" to update "Configuration Item" of closed incidents.

Trying to test it for one incident before running a update for all.

var gr = new GlideRecord('task');
gr.addQuery('number','INC2142115');
gr.query();
while(gr.next()){
gr.cmdb_ci = 'Atlas';
gr.update();
}

the following error is seen in  work notes of the ticket.

Can someone please help to find the reason for the update not going through.

 

1 ACCEPTED SOLUTION

You should use sys_id or displayValue.

var gr = new GlideRecord('task'); // why are you referring to task table here instead of incident
gr.addQuery('number','INC2142115');
gr.query();
while(gr.next()){
gr.setDisplayValue('cmdb_ci', 'Atlas');

gr.setWorkflow(false);
gr.update();

}


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

7 REPLIES 7

Nia McCash
Mega Sage
Mega Sage

cmdb_ci is a reference field so you need to provide the sys_id of the CI record you want to reference, not the string name.

 

var gr = new GlideRecord('task');
gr.addQuery('number','INC2142115');
gr.query();
while(gr.next()){
gr.cmdb_ci = 'cb103af8db6723002f153c8f9d9619af'; //<-- sys_id here
gr.update();
}

Sunny Joel
Giga Expert

Perfect.

That went through.

Thank you very much guys.

asifnoor
Kilo Patron

Since cmdb_ci is a reference field, you have to update like this.

Also, you need to update incident table, not task table.

var gr=new GlideRecord("incident");
gr.addQuery("number","INC2142115");
gr.query();
if(gr.next()) {
  var gr1=new GlideRecord("cmdb_ci");
  gr1.addQuery("name","Atlas");
  gr1.setLimit(1);
  gr1.query();
  if(gr1.next()) {
    gr.cmdb_ci=gr1.sys_id;
    gr.update();
  }
}

I tried in my dev instance nad it's working fine.

Mark the answer as correct once it worked.