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

Ankur Bawiskar
Tera Patron
Tera Patron

@IosifH 

both the variables have same name and hence the issue and it's clearing only the 1st one.

the 2nd variable is list collector and give it some other name and enhance your script as this and it will clear both

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.clearValue("requested_for");
    g_form.clearValue("listVariableName");// give your variable name here
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for your reply! Although, I would like to clear the values from one single variable of type 'requested_for', which in screenshots has the name 'requested_for'. This variable has the checkbox 'enable_also_request_for' in order to be able to request this catalog item for multiple users at once. 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

I'll try to reproduce. Maybe its a weird thing of the requested_for variable type.

 

It's not what Ankur is mentioning in his reply. Since its about the requested_for variable type.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

MaxMixali
Giga Guru

ServiceNow – Clear ALL users from a ‘Requested For’ variable with “Enable also request for”

Problem
-------
You have a Catalog Client Script (onChange) that calls:
g_form.clearValue('requested_for');
When your checkbox (remove_or_add_checkbox) changes, only the **first** user clears. The remaining users stay.

Why
---
When you enable **“Enable also request for”** on a **Requested For** variable, ServiceNow uses **two variables** under the hood:
1) `<var_name>` → the primary Requested For (single user)
2) `<var_name>_also_request_for` → the additional users (comma-separated sys_ids / list collector)

Clearing only `<var_name>` will remove just the first/primary user, not the “also request for” list.

Solution (clear both)
---------------------
Update your onChange script to clear **both** variables. Use a defensive pattern that works in Classic and Service Portal.

Example (onChange on remove_or_add_checkbox):
```javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;

// Your requested for variable name:
var rf = 'requested_for';
var rfAlso = rf + '_also_request_for';

// Clear the primary requested_for
if (g_form.getControl(rf)) {
g_form.clearValue(rf);
} else {
// Fallback
g_form.setValue(rf, '');
}

// Clear the “also request for” list (this is the part you were missing)
if (g_form.getControl(rfAlso)) {
g_form.clearValue(rfAlso);
} else {
// In Service Portal, setValue('') reliably clears list collector values
g_form.setValue(rfAlso, '');
}
}
```

Generalized helper (if you have multiple such 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, '');
}
}
```

Additional tips
---------------
- Ensure the **onChange** script is bound to the checkbox variable (`remove_or_add_checkbox`) and **UI Type = All** (so it runs in Service Portal / Workspace as well).
- If the Requested For variable is **mandatory**, clearing it will prompt the user to re-select; that’s expected.
- If you use multiple Requested For variables on the same item, call `clearRequestedForPair('<var_name>')` for each.
- For debugging in Portal, use browser dev tools console and type `g_form.getValue('requested_for_also_request_for')` to confirm values are cleared.

Summary
-------
To clear *all* selected users, you must clear both the primary Requested For variable and its companion `<var_name>_also_request_for` list. The snippets above implement this reliably in both Classic UI and Service Portal.