- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2023 06:26 AM
Hi All,
I want to work with field 'State' in Incident form:
Currently no matter which state is chosen, I can choose every available option - 'In progress', 'Resolved' and so on
I want to change choices in 'State' field depends on currently chosen value in field 'State', for example:
Currently I have state 'New' and I should have, from 6, only 3 options available to change the state:
For example only: Active, On hold, Closed
Do you how can I achieve this?
It would beeasier if it would depend on other field but here everything happens in the same field so that is why I am wondering
Thanks in advance for help!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2023 08:48 AM
Hi Kasia,
You will want to do this with a client script I think, you may need just an onload script OR an onload and an onchange.
if (newValue == 'In Progress' ) {
g_form.removeOption('state', 'Resolved');
//Repeat above line for other states to be removed
}
You can also use the below to add options back in, but be mindful that if the option is already in the list, I think it adds a duplicate.
g_form.addOption('state', 'Pending');
The example I first added uses "newValue" (because it would be an onchange client script) but if you were using an onLoad client script you would want to replace that with g_form.getValue('state')

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2023 08:48 AM
Hi Kasia,
You will want to do this with a client script I think, you may need just an onload script OR an onload and an onchange.
if (newValue == 'In Progress' ) {
g_form.removeOption('state', 'Resolved');
//Repeat above line for other states to be removed
}
You can also use the below to add options back in, but be mindful that if the option is already in the list, I think it adds a duplicate.
g_form.addOption('state', 'Pending');
The example I first added uses "newValue" (because it would be an onchange client script) but if you were using an onLoad client script you would want to replace that with g_form.getValue('state')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2023 05:40 AM
Hi!
Thank you!
I used your solution (with onChange) and generally it works fine but now I have another issue:
Example:
I have a client script as below (only a part of it):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '1') {
g_form.removeOption('state', '5');
g_form.removeOption('state', '6');
}
if (newValue === '2') {
g_form.removeOption('state', '1');
g_form.removeOption('state', '7');
So when I have chosen '1' then it removes values 5 and 6, so I can see options: 1,2,3,4,7 - it is ok
But then I choose value '2' and then I can see only option 2,3, but I should see 2,3,4,5,6
So it looks like when options are removed then they are not come back in change of state
I was trying to use in the same time 'addOption' but it doesn't work
Any suggestion?
Thanks in advance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-07-2023 06:47 AM
Hi Kasia,
Apologies addOption is formatted slightly different than I said, you need to give the value, display value and the order. (Docs GlideForm addOption | ServiceNow Developers)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Add all states back into choice list
g_form.addOption('state', '1', 'New', 1);
g_form.addOption('state', '2', 'In Progress', 2);
g_form.addOption('state', '3', 'On Hold', 3);
g_form.addOption('state', '6', 'Resolved', 4);
g_form.addOption('state', '7', 'Closed', 5);
g_form.addOption('state', '8', 'Cancelled', 6);
//Below if uncommented clears the entire list
//g_form.clearOptions('state');
if (newValue === '1') {
g_form.removeOption('state', '5');
g_form.removeOption('state', '6');
}
if (newValue === '2') {
g_form.removeOption('state', '1');
g_form.removeOption('state', '7');
}
}
Something like the above would work, whenever the onchange script runs all options are added back in, then each of your if statements will remove the options that are unavailable at that point. You could reformat that into a switch statement if you felt like it was tidier.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-11-2023 12:36 AM - edited ‎09-11-2023 12:38 AM
I haven't never use switch statement, don't have experience in that..
Currently I have Client Script like below: