Auto-populate Configuration Item field on incident form with certain conditions :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2023 04:31 AM
I want to Auto-populate CI field on incident form from reference table 'cmdb_ci_pc_hardware'(this table consists of PC assigned to users) with some conditions :
1. If user has one device then auto-populate CI field .
2.if user has multiple devices assigned then clear the CI field .
3 . It should not work on old incidents which is already created .
Howvever we have tried this with script include and onchange client script but it did't work.
Please suggest.
function onChange(control, oldValue, newValue, isLoading) {
// if (newValue == '') {
// g_form.addInfoMessage('newValue ');
// g_form.setVisible('cmdb_ci', true);
// g_form.clearValue('cmdb_ci');
// return;
// }
var caller = g_form.getValue('caller_id');
if (isLoading && oldValue != '') { // newValue == ''
alert('Old value running ');
var ref = g_form.getReference('cmdb_ci', pullAttributes);
// g_form.addInfoMessage('Old Value ' + g_form.getValue('cmdb_ci'));
// g_form.setVisible('cmdb_ci', true);
// g_form.clearValue('cmdb_ci', false);
}
else if (isLoading && newValue != '') { // && oldValue == ''
alert('New Value running ');
var ga = new GlideAjax('Inc_ci');
ga.addParam('sysparm_name', "getCIList2");
ga.addParam('sysparm_role', caller);
ga.addParam('sysparm_sysID', g_form.getUniqueValue());
alert('Caller ' + caller);
alert('SysID ' + g_form.getUniqueValue());
ga.getXMLAnswer(function(answer) {
g_form.addInfoMessage('Answer ' + answer);
if (answer != '' && answer != 'List') { // Ci available - only 1
g_form.setDisplay('cmdb_ci', true);
g_form.setValue('cmdb_ci', answer);
} else if (answer == 'List') { // Ci available - multiple
g_form.clearValue('cmdb_ci');
g_form.setVisible('cmdb_ci', true);
} else if (answer == '') { // Ci avaialble - No
g_form.clearValue('cmdb_ci');
}
});
}
}
function pullAttributes(ref) {
g_form.addInfoMessage('pullAttributes running');
g_form.setValue('cmdb_ci', ref.cmdb_ci);
}
getCIList2: function() {
gs.log('running CI ');
var myDateTime = new GlideDateTime();
myDateTime.setValue('2023-08-01 00:00:00');
var user_id = this.getParameter('sysparm_role');
var record = this.getParameter('sysparm_sysID');
gs.log('running CI2');
var gr = new GlideRecord('incident');
gr.addQuery('sys_id', record);
gs.log('running inc ' + record);
gr.query();
if (gr.next()) {
gs.log('running gr.next() ');
var createdDateTime = new GlideDateTime(gr.sys_created_on);
gs.log('running ' + createdDateTime);
if (createdDateTime.before(myDateTime))
return 'false';
else {
var ci = new GlideRecord('cmdb_ci_pc_hardware');
ci.addQuery('assigned_to', user_id);
gs.log('running user ' + user_id);
ci.query();
if (ci.next()) {
if (ci.getRowCount() == 1) {
//gs.log('JT: Row count is ' + ci.getRowCount());
return ci.getValue('sys_id');
}
return "List";
}
return "";
}
}
},
type: 'Inc_ci'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2023 11:15 PM
Hello @Diana Rodriguez ,
There are some changes required in your script
Changes in client script :
Changes in Script include :
I would recommend to understand how it works first Script Include and GlideAjax ServiceNow Coding Demo with Examples
Kindly mark correct and helpful if applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2024 01:39 AM
I couldn't really get these to work since there was some misalignment between New records and OnChange. These are the scripts I got to work with extra logging so remove once tested in Dev/Test. If you change the caller in the Incident then the CI changes so adapt to your own process.
Script include:
var IncCi = Class.create();
IncCi.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCIForCaller: function() {
gs.info("Entering getCIForCaller function");
var user_id = this.getParameter('sysparm_role');
var record = this.getParameter('sysparm_sysID');
gs.info("Received parameter - user_id: " + user_id);
gs.info("Received parameter - record: " + record);
if (!user_id) {
gs.info("user_id is blank, returning false");
return false;
}
var ci = new GlideRecord('cmdb_ci_computer');
ci.addQuery('assigned_to', user_id);
ci.addQuery('operational_status', "1")
ci.addQuery('sys_class_name', "cmdb_ci_computer")
ci.query();
gs.info("Queried cmdb_ci table with assigned_to: " + user_id);
if (ci.getRowCount() == 1 && ci.next()) {
gs.info("Found CI with sys_id: " + ci.getValue('sys_id'));
return ci.getValue('sys_id');
} else if (ci.next()) {
gs.info("Found >1 CI with sys_id: " + ci.getValue('sys_id'));
return ci.getValue('sys_id');
} else {
gs.info("No CI found or multiple CIs found, returning false");
}
return false;
}
});
Onchange script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var caller = g_form.getValue('caller_id');
console.log("Caller ID: " + caller); // Log the caller value
var currentCI = g_form.getValue('cmdb_ci'); // CI field name
if (caller) {
console.log("Caller ID 2: " + caller); // Log the caller value
var ga = new GlideAjax('IncCi');
ga.addParam('sysparm_name', 'getCIForCaller');
ga.addParam('sysparm_role', caller);
ga.addParam('sysparm_sysID', g_form.getUniqueValue());
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.setValue('cmdb_ci', answer);
} else {
g_form.clearValue('cmdb_ci');
}
});
} else {
console.log("Caller ID is blank, not making GlideAjax call");
}
}