The CreatorCon Call for Content is officially open! Get started here.

Enter record into Affected CIs

Nagendra Babu32
Tera Contributor

I have created list type field on change form as shown in below image..

 

I need to add this values into Affected CIs related tab. 

I have made  changes in business rule sync ci to affected cis  as in below image

NagendraBabu32_1-1697209649647.png

I facing an error in getting values from list type into script.  

Please help me to get values from list type into array values in business rule script.

1 ACCEPTED SOLUTION

If those are actually sys_ids of records found on the cmdb_ci table, meaning that is the table used as a Reference in this list field, then change your script like this to account for current.u_affected_companies containing a comma-separated list of sys_ids.  The addQuery on line 11 should look like this:

 

rec.addQuery('ci_item', 'IN', previous.u_affected_companies);

 

Then inside the addCurrentCI function, that all needs to be wrapped in a for loop to process each value:

 

function addCurrentCI() {
    //Create Affected CI record for the Outage(s) associated with this incident and current CI
    var ciArr = current.u_affected_companies.split(',');        
    for (var i=0; i<ciArr.length; i++){
        var rec = new GlideRecord('task_ci');
        rec.addQuery('task', current.sys_id);
        rec.addQuery('ci_item', ciArr[i]);
        rec.query();
        if (rec.next()) {
            //Affected CI already found, so don't add a duplicate
            return;
        }
        rec.initialize();
        rec.task = current.sys_id;
        rec.ci_item = ciArr[i];
        rec.insert();
    }
}

 

 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

The ci_item field on the task_ci table is a reference to the Configuration Item (cmdb_ci) table, so you need to populate it with a sys_id of a record found on this table, not on the core_company table which is what I would guess you used as the Reference for your new list field.

I was getting sys ids from list values as  

Script: 74d8108297b5311009247076f053af78,ae0b504a97b5311009247076f053affa

 I need  to push these into array which I am unable to do and unable to split those values.

If those are actually sys_ids of records found on the cmdb_ci table, meaning that is the table used as a Reference in this list field, then change your script like this to account for current.u_affected_companies containing a comma-separated list of sys_ids.  The addQuery on line 11 should look like this:

 

rec.addQuery('ci_item', 'IN', previous.u_affected_companies);

 

Then inside the addCurrentCI function, that all needs to be wrapped in a for loop to process each value:

 

function addCurrentCI() {
    //Create Affected CI record for the Outage(s) associated with this incident and current CI
    var ciArr = current.u_affected_companies.split(',');        
    for (var i=0; i<ciArr.length; i++){
        var rec = new GlideRecord('task_ci');
        rec.addQuery('task', current.sys_id);
        rec.addQuery('ci_item', ciArr[i]);
        rec.query();
        if (rec.next()) {
            //Affected CI already found, so don't add a duplicate
            return;
        }
        rec.initialize();
        rec.task = current.sys_id;
        rec.ci_item = ciArr[i];
        rec.insert();
    }
}

 

 

Alka_Chaudhary
Mega Sage
Mega Sage

To delete Previous Records :

Use this query:- 

rec.addEncodedQuery('ci_itemIN'+ previous.u_affected_companies);

 

 

To create record for each CI :-

Use the below script:

function addCurrentCI() {
    var ci = current.u_affected_companies.split(',');
    for (var i in ci) {
        var rec = new GlideRecord('task_ci');
        rec.addQuery("task", current.sys_id);
        rec.addQuery("ci_item", ci[i]);
        rec.query();
        if (rec.next()) {
            return;
        } else {
            var newrec = new GlideRecord('task_ci');
            newrec.initialize();
            newrec.task = current.sys_id;
            newrec.ci_item = ci[i];
            newrec.insert();
        }
    }
}