Validation of LIST type field from onChange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:46 AM - edited 10-11-2023 04:49 AM
Hello All,
Hope you are doing well.
I have a form on incident table to create incidents for multiple users at once. To address this,
there is a 'user' field which is a referance field to user table. There is another field as 'multiple users' which is a LIST type field that allows multiple users to be selected.
I am working on a validation part where if same user (the one selected on single user field) is selected in ''multiple users' , it must remove that user from 'multiple users' field. The code is part of onChange script on incident form.
I am able to figure out the same user but unable to remove this particular user from the LIST type field
ex-
In 'User' field, I chose 'Jack Sparrow'
In 'multiple users' field, If I choose 'Harry Potter', 'Jack Sparrow', it must immediately remove 'Jack Sparrow', as it already selected in User field.
The code is part of onChange script on incident form. I am able to parse multiple users using split by , on the LIST field.
Require your expert inputs on this issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 04:54 AM
Since both fields store sys_ids you can keep this processing in the client with string manipulation. After you split the list contents, loop through them with a for loop. Inside the loop, if that current value is not the same as the reference field, push it to a new array, then set the list field value to the new joined array. Give it a try and post your script if you get stuck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 07:05 PM
var arr = [];
var user_list = g_form.getValue('multiple_users').split(","); // multiple users LIST type field
for (var i = 1; i <=user_list .length; i++) {
if (g_form.getValue('user') != user_list[i]) {
arr.push(user_list[i].toString());
} else {
alert(Please choose a unique value. ");
}
}
g_form.setValue(multiple_users, arr);
The logic you explained isnt working. Duplicate not getting cleared out
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 05:05 AM
You have a couple of logic errors and an extra space in your for statement that is causing it to fail. You'll also want to wrap this in an if condition, testing if the single value is found in the list so it doesn't run on every update - since this is running onChange of the multiple users field and updating the same it will constantly run/update. Also note the alert text inside of quotes. The array join is implied when not stated, but better safe than sorry... Since this is running onChange of multiple_users you could replace the getValue for this field to newValue
var arr = [];
var user_list = newValue.split(","); // multiple users LIST type field
var usr = g_form.getValue('user');
if (newValue.indexOf(usr) > -1) { //only do this if user reference is found in user list
for (var i = 0; i < user_list.length; i++) {
if (usr != user_list[i]) {
arr.push(user_list[i].toString());
} else {
alert('Please choose a unique value.');
}
}
g_form.setValue(multiple_users, arr.join(","));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2023 05:25 AM
hi @Snehal13
on change client script,