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
1 REPLY 1

Tanushree Maiti
Giga 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 mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: