VaranAwesomenow
Mega Sage

Step 1: Go to deleted records and find out which all records got deleted because of the deleted configuration item record.

Usually you will see records from cmdb_ci ( or any of the child tables depending on which CI got deleted) and the task_ci records corresponding to that CI.

Step 2.Using the undelete option, restore the cmdb_ci( or any of the child tables depending on which CI got deleted) records first and then undelete the other related records like task_ci or other m2m table records.

Step 3. Go to sys_history_line table and find out which task records had a configuration item on them and not have them anymore in order to get that data run below query in background script ( strongly recommended to run in dev or sub prod instances and validate the data before doing it on prd instance)

var query = 'cmdb_ci=';

var cmdb_ci_sys_id = '';

var grTask = new GlideRecord('task');

grTask.addEncodedQuery(query);

grTask.query();

while(grTask.next()) {

cmdb_ci_sys_id = getCI(grTask.sys_id);

if(cmdb_ci_sys_id != '')

  gs.print(grTask.number + "|" + grTask.sys_id + "|"   +cmdb_ci_sys_id );

}

function getCI(task) {

  var query = 'set.id='+ task+'^label=Configuration item';

  var cmdb_ci_sys_id = '';

  //gs.print(query);

  var grHist = new GlideRecord('sys_history_line');

  grHist.addEncodedQuery(query);

      grHist.orderByDesc('update_time');

      grHist.query();

      if(grHist.next()) {

      cmdb_ci_sys_id = grHist.new_value;

      }

return cmdb_ci_sys_id ;

}

You should receive an output like this

[0:00:02.426] Script completed in scope global: script


*** Script: INC0000040|471d4732a9fe198100affbf655e59172|0c43b896c6112275019abd2b2b93f464

the output is a | separated text, now this can be easily copied to excel and converted to 3 columns, task #, task sys_id and cmdb_ci sys_id.

Step 4: Next we have 2 options 1. either update the task record via script or manually do it.

If you want to it via script then the above script can be tweaked to do the same.

Below script provides 2 options 1. use it to verify the existence of cmdb_ci sys_id by setting the doIt = false, use it to update the task record by setting the doIt = true

var doIt = false; // use this to verify the data

//var doIt = true; // use this to update the data

checkTasks(doIt);

function checkTasks(doIt) {

  var query = 'cmdb_ci=';

  var cmdb_ci_sys_id = '';

  var grTask = new GlideRecord('task');

  grTask.addEncodedQuery(query);

  grTask.query();

  while(grTask.next()) {

  cmdb_ci_sys_id = getCI(grTask.sys_id);

  if(cmdb_ci_sys_id != '') {

  gs.print(grTask.number + "|" + grTask.sys_id + "|"   +cmdb_ci_sys_id );

  if(doIt) {

  grTask.cmdb_ci = cmdb_ci_sys_id;

  grTask.setWorkFlow(false);

  grTask.update();

  }

  }

  }

}

function getCI(task) {

  var query = 'set.id='+ task+'^label=Configuration item';

  var cmdb_ci_sys_id = '';

  //gs.print(query);

  var grHist = new GlideRecord('sys_history_line');

  grHist.addEncodedQuery(query);

  grHist.orderByDesc('update_time');

  grHist.query();

  if(grHist.next()) {

  cmdb_ci_sys_id = grHist.new_value;

  }

  return cmdb_ci_sys_id ;

}

4 Comments