- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 12:07 PM - edited 05-22-2024 10:58 PM
Under a multi-row variable set (environment_selection) two select box fields i.e., select_environment , select_role is present.
A requester could request different roles for the same environment this can't be allowed.
For ex. I could select both the user and the viewer roles for ARCS UAT. Please check below.
However, only one role per environment is allowed to be selected.
If multiple roles are selected for the same environment, an alert must come saying that the selected environment is already added and clear the chosen environment field value.
I'm not sure how to check two variables. Please help me with this.
Please help me with this.
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 08:04 AM
what type of client script it is?
onSubmit on catalog item right?
try using alert instead of g_form
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-23-2024 12:48 AM
Hi @Ankur Bawiskar,
I didn't understand the comments here: instead of "type" give your key name which you want to be unique. We discussed that select_environment needs to be unique
var uniqueArray = removeDuplicates(arrayWithDuplicates, "select_environment"); // here instead of "type" give your key name which you want to be unique
I tried the script given below
function onSubmit() {
var arrayWithDuplicates = JSON.parse(g_form.getValue('environment_selection')); // variable set name
var uniqueArray = removeDuplicates(arrayWithDuplicates, "select_environment"); // here instead of "type" give your key name which you want to be unique
var oldArrayLength = arrayWithDuplicates.length;
var newArrayLength = uniqueArray.length;
if (newArrayLength < oldArrayLength) {
g_form.addErrorMessage('Multiple roles for same environment is not allowed');
return false;
}
}
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for (var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for (i in lookupObject) {
newArray.push(lookupObject[i]);
}
return newArray;
}
but it is not working still we can submit with multiple roles for the same environment.
Please help me if i missed anything here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 02:14 AM
did you debug by adding alert in the above script and see the outcomes?
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-23-2024 04:26 AM
Hi @Ankur Bawiskar,
Trying to debug by adding alerts and it is returning [object Object],[object Object],[object Object]
function onSubmit() {
var arrayWithDuplicates = JSON.parse(g_form.getValue('environment_selection')); // variable set name
alert('environments_selected' + arrayWithDuplicates );
var uniqueArray = removeDuplicates(arrayWithDuplicates, "select_environment");//give key name
alert('uniqueArray' + uniqueArray);
var oldArrayLength = arrayWithDuplicates.length;
var newArrayLength = uniqueArray.length;
alert('oldarraylength' + oldArrayLength);
alert('newArrayLength' + newArrayLength);
if (newArrayLength < oldArrayLength) {
g_form.addErrorMessage('Multiple roles for same environment is not allowed');
return false;
}
}
function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject = {};
for (var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];
}
for (i in lookupObject) {
newArray.push(lookupObject[i]);
}
alert('newArray' + newArray);
return newArray;
}
Not sure of what is happening wrongly here. Unable to find the issue.
Can you please help me. I'm attaching all the logs
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2024 05:08 AM
the if condition is getting satisfied then it should go inside the IF
if (newArrayLength < oldArrayLength) {
it's not going and not showing the error message on form?
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-23-2024 05:41 AM
Even though if condition is getting satisfied. We are not getting the error message.
Any thoughts on this please.