List Collector onChange – Detect only newly added sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Team,
I am facing an issue with a List Collector variable in ServiceNow (Catalog Client Script – onChange) and need guidance.
Requirement
I want to show an info message only when a specific sys_id is selected for the first time in the list collector.
Current Behavior
- When I select the target value (sys_id), the message is displayed correctly
- However, when I select additional values afterward, the message is triggered again
- This happens because the selected sys_id is already present in the list
What I Tried
- Using indexOf() on newValue
- Comparing oldValue and newValue
- Using array split logic
- Even tried string-based comparisons without split
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @SiddeswaraJ
Try this..
To achieve this, use a Display Business Rule on the sc_req_item (Requested Item) table. And onchange client script
This script retrieves the variable's current value and compares it to the value stored in the database for that same RITM record.
- Create a new Business Rule with When: display and use the following script:
(function executeRule(current, previous /*null when async*/) {
var varName = 'your_list_collector_variable_name';
var targetSysId = 'sys_id_of_specific_value';
var currentVal = current.variables[varName].toString();
var ritmGr = new GlideRecord('sc_req_item');
var oldVal = '';
if (ritmGr.get(current.sys_id)) {
oldVal = ritmGr.variables[varName].toString();
}
var currentContains = currentVal.indexOf(targetSysId) > -1;
var oldContains = oldVal.indexOf(targetSysId) > -1;
if (currentContains && oldContains) {
g_scratchpad.flag_match = false;
} else {
g_scratchpad.flag_match = true; //first time added
}
})(current, previous);
2. Create onChange Client script on List collector variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var targetSysId = 'YOUR_SPECIFIC_SYS_ID_HERE';
if (newValue && g_scratchpad.flag_match && newValue.indexOf(targetSysId) > -1)
{
g_form.addInfoMessage('Your custom info message goes here.');
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@SiddeswaraJ ,
Try this script once
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return; // Don't run on page load
var TARGET_SYS_ID = 'your_specific_sys_id_here';
// Convert comma separated string to arrays
var newValues = newValue ? newValue.split(',') : [];
var oldValues = oldValue ? oldValue.split(',') : [];
// Check if target sys_id is in newValue
var isInNew = newValues.indexOf(TARGET_SYS_ID) > -1;
// Check if target sys_id was NOT in oldValue
var wasInOld = oldValues.indexOf(TARGET_SYS_ID) > -1;
// Show message ONLY when added for the first time
if (isInNew && !wasInOld) {
g_form.showFieldMsg(
'your_variable_name',
'Your info message here',
'info'
);
}
// Hide message if sys_id is removed
if (!isInNew && wasInOld) {
g_form.hideFieldMsg('your_variable_name', true);
}
}
If my response helped, mark it as helpful and accept the solution.
Thanks,
Dinesh