List Collector onChange – Detect only newly added sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours 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
2 hours 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.');
}
}