Display the List of affected CIs of Incident in a custom Field in Incident Record.

MihirG
Tera Contributor
I kindly request your assistance in reviewing the following Client Script and Script Include. The objective is to display the CIs name associated with the Incident Record in custom field named as "List of associated CIs:" on the incident form. 
 
Client Script:
 
Name: ShowaffectedCIs
Table: Incident[incident]
Active: checked
Global: checked
UI Type: All
Type: OnLoad
 
Script:
function onLoad() {
    var finalArray = [];
    var ga = new GlideAjax('CIsIncident');
    ga.addParam('sys_parm_name', 'affectedCIs');
    ga.addParam('sys_parm_INCsys', g_form.getValue('sys_id'));
    ga.getXMLAnswer(callback);

    function callback(res) {
        alert(res);
        var obj = JSON.parse(res);
        for (var i = 0; i < obj.length; i++) {
            finalArray.push('task: ' + obj[i].taskName + 'CI: ' + obj[i].name);
        }
        g_form.setValue('u_list_of_associated_cis', finalArray.join('\n'));
    }
}
 
ScriptInclude:
 
Name: CIsIncident
Client callable: checked
Active: checked
 
Script: 
var CIsIncident = Class.create();
CIsIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    affectedCIs: function() {
        var ciList = [];
        var sysID=this.getParameter('sys_parm_INCsys');
        var gr = new GlideRecord('task_ci');
        gr.addQuery('task',sysID);
        gr.query();
        while (gr.next()) {
            var ciDetails={};
            ciDetails.taskName=gr.task.getDisplayValue();
            ciDetails.name=gr.ci_item.getDisplayValue();
            ciList.push(ciDetails);
        }
        return JSON.stringify(ciList);
    },
    type: 'CIsIncident'
});
 
I have implemented this logic using Client Script(OnLoad) and Script Include(Client callable) used task_ci record. Your expert evaluation of the code—whether it is correct or requires adjustments—would be greatly appreciated. Shared the attachment with the same.
2 REPLIES 2

Ravi Gaurav
Giga Sage
Giga Sage

Hi @MihirG 

can you use the below revise script :-

Client Script :-

function onLoad() {
var finalArray = [];
var ga = new GlideAjax('CIsIncident');
ga.addParam('sys_parm_name', 'affectedCIs');
ga.addParam('sys_parm_INCsys', g_form.getValue('sys_id'));
ga.getXMLAnswer(callback);

function callback(res) {
if (res) {
try {
var obj = JSON.parse(res);
for (var i = 0; i < obj.length; i++) {
finalArray.push('Task: ' + obj[i].taskName + ', CI: ' + obj[i].name);
}
g_form.setValue('u_list_of_associated_cis', finalArray.join('\n'));
} catch (e) {
console.error('Error parsing response:', e);
}
} else {
console.warn('No associated CIs found.');
g_form.setValue('u_list_of_associated_cis', 'No associated CIs.');
}
}
}

script Include:-

var CIsIncident = Class.create();
CIsIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
affectedCIs: function() {
var ciList = [];
var sysID = this.getParameter('sys_parm_INCsys');
if (!sysID) {
return JSON.stringify([]);
}

var gr = new GlideRecord('task_ci');
gr.addQuery('task', sysID);
gr.query();

while (gr.next()) {
var ciDetails = {};
ciDetails.taskName = gr.task.getDisplayValue();
ciDetails.name = gr.ci_item.getDisplayValue();
ciList.push(ciDetails);
}

return JSON.stringify(ciList);
},

type: 'CIsIncident'
});

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

MihirG
Tera Contributor

Hi Ravi,

I wanted to bring to your attention that the script you provided earlier does not seem to be functioning as expected. I have already attempted the suggested solution, but unfortunately, it has not yielded the desired results.

 

Could you please explore alternative approaches or provide additional guidance to address this issue? Your assistance would be greatly appreciated.