sync CI on Change Task to Affected CI list on Change Request

taddparris
Kilo Expert

We would like to start having the Configuration Item on a Change Task sync to the Affected CI related list on the Change Request.  

If a Change Request's CI was (AAA) and there was a Change Task for (BBB), we want both AAA and BBB to show up in the Affected CI related list.   The best approach appears to have a business rule that will create a new form on the Task_CI table, just having some difficulties doing it.   I took an existing Business Rule "Sync CI with Affected CIs" and made it specific for the Change Task table and made some modifications.   Below is the script and some information about the tables and fields being used.   If the form uses same Task ID as the Change Request, it should show up on the Change Request related list.   Just need help in making sure it is creating that Task_CI record.

Affected CI's Table = Task_CI

Change Request Table = Change_Request

Change Task Table = Change_Task

Business Rule runs on the Change Task Table.

current.cmdb_ci.changes()

if (!previous.cmdb_ci.nil())

    removePreviousCI();

if (!current.cmdb_ci.nil())

    addCurrentCI();

function removePreviousCI() {

    // Delete Affected CI records for this task and previous CI

    var rec = new GlideRecord('task_ci');

    rec.addQuery('change_task.change_request', current.sys_id);

    rec.addQuery('ci_item', previous.cmdb_ci);

    rec.query();

    while (rec.next())

          rec.deleteRecord();

}

function addCurrentCI() {

    //Create Affected CI record for this task and current CI

    var rec = new GlideRecord('task_ci');

    rec.addQuery('change_task.change_requestchange_request', current.sys_id);

    rec.addQuery('ci_item', current.cmdb_ci);

    rec.query();

    if (rec.next())

          return;

    rec.initialize();

    rec.change_task = current.change_request;

    rec.ci_item = current.cmdb_ci;

    rec.insert();

}

Currently I am getting a looped error which looks like this

find_real_file.png

Thank you for any help you can provide

10 REPLIES 10

Michael Fry1
Kilo Patron

You can't remove it from the task_ci (Affected CI's) if the CI is in the Configuration Item on the Change form. So you probably need to check for that, and if it's not, then execute your RemovePreviousCi routing. At least that's why I'm thinking is the problem. Does your add work fine?


Sashi K1
Kilo Guru

Hi


Did you check for any Delete BR on task_ci table? I believe you are recursively trying to remove CIs from task_ci which is causing a Delete BR to fire recursively.



Thanks


I found the issue for the error and you were correct.   Started messing with an older Business Rule that I found on this site and got it working.



function onBefore(current, previous) {


    //This function will be automatically called when this rule is processed.


    var chgID = current.change_request;  


var config = current.cmdb_ci;  


 


var taskci = new GlideRecord('task_ci');  


taskci.task = chgID;  


taskci.ci_item = config;  


taskci.insert ();


}



However, now if I change the Configuration Item on the same Change Task it does not remove the old record.


It should work without initialize(), just check by adding. Also check any insert BR on task_ci