Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clear all values of a multi-user variable of type requested_for

IosifH
Tera Contributor

I am building a catalog item that includes a multi-user variable of type 'requested_for'. The checkbox 'enable_also_request_for' is enabled for this variable. There is a a catalog client script (onChange) that clears the value of requested_for when another variable changes. Here is the script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.clearValue("requested_for");
}

The current behaviour  is that when 'remove_or_add_checkbox' changes value only the first row/user from requested_for is being cleared (where snowflake.reporting user appears).

The expected behaviour is that I'd like all users (including admin, data management & AWS clouddiscovery) to be cleared when 'remove_or_add_checkbox' has another value (e.g. when switching from 'add' to 'remove'). Please see the attached screenshots for reference.

IosifH_0-1761229043994.pngIosifH_1-1761229059906.png

 

 

Has anyone experienced this issue or knows how to clear all selected users in a multi-user variable via client script? Thanks in advance!

1 ACCEPTED SOLUTION

lauri457
Giga Sage

I can't see my reply for some reason so I'll repost it

 

I looked at the snScAlsoRequestFor directive but couldn't figure out a "good" way to achieve what you are looking for. However this did work, the also_request_for_value prop in the requested_for field stores a string of the sysids; emptying this seems to stop the creation of the extra RITMs. Jquery and select2 can be used to wipe the field. Also untick isolate script field on the client script

 

g_form.getField("requested_for").also_request_for_value = ""; 
this.$('#sp_formfield_sn_sc_also_request_for').select2('data', null);

  

View solution in original post

8 REPLIES 8

IosifH
Tera Contributor

This reply looks like it's AI generated. I tried anyway the proposed solution but it's not working.

MaxMixali
Giga Guru

ServiceNow – Clear ALL users in a multi-user 'requested_for' variable when using “Enable also request for”
==========================================================================================================

Problem
-------
You have a Catalog Item with a **multi‑user variable** of type **requested_for**. You enabled the checkbox **“Enable also request for”** and you clear the value with a client onChange script:
g_form.clearValue('requested_for');
Only the **first (primary)** user is cleared; the additional users remain.

Why it happens
--------------
When you enable **Enable also request for** on a Requested For variable, ServiceNow actually stores values in **two variables**:
1) `<var_name>` → the **primary** requested_for (single user)
2) `<var_name>_also_request_for` → the **additional users** (list collector / comma-separated sys_ids)

Clearing only `<var_name>` leaves `<var_name>_also_request_for` untouched, so extra users stay selected.

Fix – clear BOTH variables
---------------------------
Update your onChange script to clear both the primary and the “also request for” list. Use a defensive pattern that works in Classic UI, Service Portal and Workspace.

Example (onChange on your controlling variable, e.g., remove_or_add_checkbox):
```javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;

// Name of your Requested For variable
var rf = 'requested_for';
var rfAlso = rf + '_also_request_for';

// Clear primary requested_for (single user)
if (g_form.getControl(rf)) {
g_form.clearValue(rf);
} else {
// Fallback for SP/Workspace
g_form.setValue(rf, '');
}

// Clear the "also request for" list (ALL additional users)
if (g_form.getControl(rfAlso)) {
g_form.clearValue(rfAlso);
} else {
// In SP/Workspace, setValue('') reliably clears list collector values
g_form.setValue(rfAlso, '');
}
}
```

If your variable has a custom name, replace `'requested_for'` with that name.

Optional: helper function if you have multiple RF variables
------------------------------------------------------------
```javascript
function clearRequestedForPair(varName) {
try { g_form.clearValue(varName); } catch (e) { g_form.setValue(varName, ''); }
var also = varName + '_also_request_for';
if (g_form.getControl(also)) { g_form.clearValue(also); } else { g_form.setValue(also, ''); }
}
```
Call as: `clearRequestedForPair('requested_for');`

Checklist / gotchas
-------------------
- **UI Type = All** on the client script (so it runs in Portal/Workspace).
- If the variable is **Mandatory**, clearing it will prompt re-selection (expected).
- To verify in Portal, use the browser console and check:
`g_form.getValue('requested_for_also_request_for')` → should be empty after change.
- If you changed the variable name, confirm the system created the companion variable `<name>_also_request_for`.
- If you still see stale values on submit, add an **onSubmit** Catalog Client Script to ensure both fields are empty before submission.

Summary
-------
To clear *all* selected users when using **Enable also request for**, you must clear **both** the main Requested For variable and its companion `<var_name>_also_request_for`. The snippet above implements this reliably across Classic UI, Service Portal, and Workspace.

lauri457
Giga Sage

I can't see my reply for some reason so I'll repost it

 

I looked at the snScAlsoRequestFor directive but couldn't figure out a "good" way to achieve what you are looking for. However this did work, the also_request_for_value prop in the requested_for field stores a string of the sysids; emptying this seems to stop the creation of the extra RITMs. Jquery and select2 can be used to wipe the field. Also untick isolate script field on the client script

 

g_form.getField("requested_for").also_request_for_value = ""; 
this.$('#sp_formfield_sn_sc_also_request_for').select2('data', null);

  

IosifH
Tera Contributor

Thank you! This solved my issue!