- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 08:02 AM - edited 08-02-2023 01:39 AM
Hi,
I wonder if you could assist with my client script, let me explain the use case.
I have a request where you select an application, this application field is a refence field to our cmdb. Once you select the application you are then able to select the roles associated to this application, this field is a list collector and currently you can select all of the roles associated to the app.
We now have a requirement for 1 app to be locked down so only 1 role can be selected. This was done with a client script which I will paste below. This has been developed to work specifically for the 1 app, but I would like to make this a re-usable process.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var application = g_form.getValue('appl');
if (application == 'sys id of app') {
var maxOptions = 1;
var selectedRoles = g_form.getValue('role_select');
var selectedOptions = selectedRoles.split(',');
if (selectedOptions.length > maxOptions) {
//Remove the last item
g_form.clearValue('role_select');
g_form.addErrorMessage('You cannot select more than ' + maxOptions + ' role for this application.');
}
}
}
This code above works but I want to make it expandable for other apps. I would like to be able to come into this script and add to a variable the sys ID of any app I want to add. What would be the best way to do this?
I was initially thinking of creating an array then have the IF statement look to see if the array contains the app found in the g_form.getValue(). This has proven difficult. I have been trying indexOf() and includes(), either I have done it wrong or these are not working for what I need.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 02:44 PM
I'm thinking something like this where you have an array and just check if the current application selected is in that array.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var application = g_form.getValue('appl');
var controlApps = ['sys_id','sys_id','sys_id'];
if (controlApps.indexOf(application) != -1) {
var maxOptions = 1;
var selectedRoles = g_form.getValue('role_select');
var selectedOptions = selectedRoles.split(',');
if (selectedOptions.length > maxOptions) {
//Remove the last item
g_form.clearValue('role_select');
g_form.addErrorMessage('You cannot select more than ' + maxOptions + ' role for this application.');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2023 02:44 PM
I'm thinking something like this where you have an array and just check if the current application selected is in that array.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var application = g_form.getValue('appl');
var controlApps = ['sys_id','sys_id','sys_id'];
if (controlApps.indexOf(application) != -1) {
var maxOptions = 1;
var selectedRoles = g_form.getValue('role_select');
var selectedOptions = selectedRoles.split(',');
if (selectedOptions.length > maxOptions) {
//Remove the last item
g_form.clearValue('role_select');
g_form.addErrorMessage('You cannot select more than ' + maxOptions + ' role for this application.');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2023 02:21 AM - edited 08-02-2023 02:23 AM
Legend this worked. I had the same a part from the != -1, not having this was having the IF statement run as true if the start of the sys ID was the same. Are you able to explain what the -1 does?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2023 07:13 AM
Of course! The .indexOf() part checks for the index of the value in the string/array/etc. If it doesn't exist, the index is -1. Example:
Array = [0,1,2];
Array.indexOf(0) returns an index of 0, (1) returns 1, (2) returns 2
Array.indexOf(3) returns an index of -1 since it isn't in the array
Thus, Array.indexOf(0) != -1 returns true because the index returned is not -1 meaning it is in the array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2023 04:56 AM
Thank you!