List Collector and reference field related question

KS13
Tera Contributor

Hi, 

I have a referes:

nce type variable 'Requested for' and then I have a list collect type variable 'Additional users'.

I am trying to write a simple onChange catalog client script that can perform the following functions:

1. If the Requested for Name/user ID is the same value as the one selected in the list collector, then Clear the matched value from the List collector field 'Additional users'.

2. Display alert msg. 

3. Else the user should have the ability to continue adding new value to the 'Additional users' field and the alert message should not be displayed. 

4. The script should work in the Portal as well as Native ServiceNow. 

Any help is appreciated. 

Thanks

KS13

4 REPLIES 4

Isaac Vicentini
Mega Sage
Mega Sage

Hi KS13,

 

Try this in your Catalog Client Script (check if the name of your variable is additional_users):

 

    var requestedFor = g_form.getValue('requested_for'); // Get the value of 'Requested for' field
    var additionalUsers = g_form.getReference('additional_users'); // Get the value of 'Additional users' field

    if (additionalUsers && additionalUsers.indexOf(requestedFor) !== -1) {
        // If the selected 'Requested for' value is already in 'Additional users'
        g_form.clearValue('additional_users'); // Clear the value from 'Additional users' field
         gs.addInfoMessage("The selected user is already added to the list of additional users. It has been removed.");
    } else {
        // Display an alert message indicating successful addition
         gs.addInfoMessage("The user has been successfully added to the list of additional users.");
    }

 


Best regards,

Isaac Vicentini
MVP 2025


If my answer was helpful, mark it as Helpful or Accept as Solution.

-O-
Kilo Patron
Kilo Patron

Assuming this is not the list collector generated when checking the "Enable also request for" option for the variable of type "Requested for", I would say this is not how this should be solved.

You need to consider that events are fired only when a field is blurred, so even if an entry should be removed that will only be detected when the user filling in the form leaves the field.

Allow me a solution that better fits the SN UI paradigm:

  • - on one hand use a reference qualifier for the list collector - thus not allowing selecting unwanted users in the 1st place
  • on the other hand add an onChange Catalog Client Script for the Requested for field which would make sure that the a selected Requested for user is removed from the list collector - if present.

If my assumption is wrong, and this is about the "Also request for" list collector automatically added by the system as a result of activating the "Enable also request for" option of the variable of type "Requested for", it is not possible to manipulate the list through "regular" APIs - as far as I know.

Maddysunil
Kilo Sage

@KS13 

You can write onchange catalog client script on requested for varible:

 

var requestedFor = g_form.getValue('requested_for'); // Get the selected value of 'Requested for'
    var additionalUsers = g_form.getReference('additional_users'); // Get the values of 'Additional users'

    if (additionalUsers) {
        var matchingUser = false;
        for (var i = 0; i < additionalUsers.length; i++) {
            if (additionalUsers[i].value == requestedFor) {
                matchingUser = true;
                break;
            }
        }

        if (matchingUser) {
            // Clear the matched value from 'Additional users'
            g_form.clearValue('additional_users');
            // Display alert message
            alert('The selected user is already added in the Additional Users field. It has been removed.');
        }
    }

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

KS13
Tera Contributor

Hi Sunil, 

Unfortuantely, this method is not working on my end. I tried the Portal and Native SNOW. 

I do apprciate your time though. 

Thanks

KS13