Unable to restrict a list collector field up to 10 values in onchange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 08:20 AM
Hi Team, Please find my below use case.
Case 1: group_field_type == updated
- In case there are >11 members, these members are displayed in the field and more can be added, up to 10.
- If user tries to populate 11th member, a pop-up error message is shown:
- Message Text: "Maximum allowed users is 10 only."
- "OK" Button
- After clicking "OK" Button, the 11th member is not populated.
- Field remains filled on 10 users and the user is allowed to continue.
Case 2: group_field_type == updated
- In case there are <10 members, a pop-up error message is shown:
- Message Text: "Maximum allowed is 10. Additional users were removed."
- "OK" Button
- After clicking "OK" Button, the extra users after the 10th are removed from the field and only 10 are kept.
- Field remains filled on 10 users and the user is allowed to continue.
I wrote on change client script are not working properly. I am unable to handle two alerts in same condition. Please help me @Ankur Bawiskar function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var maxOptions = 10;
var collectorName = 'group_list_collector';
var myListCollector = g_list.get(collectorName);
var selectedOptions = g_form.getValue(collectorName).split(',');
if (g_form.getValue('group_field_type') == 'updated') {
if (selectedOptions.length > maxOptions) {
var diff = selectedOptions.length - maxOptions;
if (diff > 1){
var fn= selectedOptions.length-diff;
if (!g_form._alertShown) {
alert('Maximum allowed is ' + maxOptions +'. Additional users were removed.');
g_form._alertShown = true;
}
myListCollector.removeItem(selectedOptions[fn]);
}if(diff ==1){
alert('Maximum allowed is '+ maxOptions +'.');
myListCollector.removeItem(selectedOptions[selectedOptions.length -1]);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 09:36 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 09:54 PM
Dear @UDV Prasad
For me this is a situation where it makes sense to go a few steps back.
You already seem to be deep in the actual coding.
Here are a few questions that I think are worth exploring with your business:
- Why do you want to restrict the number of the members to a constant number (or is it not even constant?)
- Why do you think people will willingly exceed a limit that the organisation imposes?
In general, I think we should Write code that is easy to delete, not easy to extend.
Or in other words: code that is never written must not be maintained.
Just my 2 cents.
Best
Daniel
If this answer was helpful, I would appreciate if you marked it as such - thanks!
Best
Daniel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 10:04 PM
so why you are removing the 11th user
let the user decide which one to remove?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 10:24 PM
As per our cases
case:1 list collector added 11 user . Alert triggered and click on okay. Same 11 user not populated list collector.
Case:2 before List collector > 10 . Alert triggered and click on Okay.after 10th Last extra users removed ..