List Collector onChange – Detect only newly added sys_id

SiddeswaraJ
Tera Contributor

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

  1. Using indexOf() on newValue
  2. Comparing oldValue and newValue
  3. Using array split logic
  4. Even tried string-based comparisons without split
2 REPLIES 2

Tanushree Maiti
Tera Patron

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.

 

  1. 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.');

             

    }

}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Dinesh Chilaka
Kilo Sage

@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