Not display all affected CIs in custom field, created in Incident record.

MihirG
Tera Contributor

Request your assistance in reviewing the following Business Rule applied after "Insert" and "Update". The objective is to display the ci_item names affected with the Incident Record in custom field named as "List of associated CIs:" in the incident form.

 

Name: affectedCIsIncident

Table: CIs Affected[task_ci]

Script:

 

(function executeRule(current, previous /*null when async*/) {
var citems=new GlideRecord('task_ci');
citems.get('task',current.task!=''?current.task:previous.task);

var ciList=[];
var gr=new GlideRecord('incident');
gr.get(current.task!=''?current.task:previous.task);
while(citems.next()){
    ciList.push(citems.ci_item.getDisplayValue());
}
gr.u_list_of_associated_cis=ciList.join(', ');
gr.update();

})(current, previous);
 
I have implemented this logic. "But all CIs names are not displaying in the custom field, one is missing and sometimes while removing the affected ci_item from task_ci incident, a new incident record automatically got created. attached an Image for your reference." Your expert evaluation of the code—whether it is correct or requires adjustments—would be greatly appreciated.
2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

The issue is likely with your non-standard use of get, combined with the use of while.  What are you really trying to accomplish getting the previous task.  Is current task ever blank?  Try something more like this:

(function executeRule(current, previous /*null when async*/) {
    var ciList = [];
    var gr=new GlideRecord('incident');
    if (gr.get(current.task) {
        var citems=new GlideRecord('task_ci');
        citems.addQuery('task', current.task);
        while (citems.next()) {
            ciList.push(citems.ci_item.getDisplayValue());
        }
        gr.u_list_of_associated_cis=ciList.join(', ');
        gr.update();
    }
})(current, previous);

If there's a case where current.task is blank, incorporate that with addQuery lines.  You can also troubleshoot this by adding log lines to see what is happening while the script is running.

MihirG
Tera Contributor

I had shared the the image of my output as attachment, kindly check, I want to display the name of CI items in custom field,  if I will add CI item under "Affected CIs" section, then automatically my custom field got updated. If I remove the CI item from the same section then automatically my custom field got updated.

 

To conclude, on removing and adding CIs from "Affected CIs" section my custom field automatically got updated with the fresh list of CIs. that's why I used both previous and current in my script. My script is running but one CI is always got missed in and rest of the CI names are displaying. My custom field names as" List of associated CIs." Kindly assist.