- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 03:55 AM
Hi all,
I am rather new to servicenow and I have started to get into scripting.
I am trying to hide state options in the incident when it is a new record to show only the state of new. However, once the record is no longer new I want to make the original options available again.
This is the code that I have now:
function onLoad() {
//hide all states except new upon first save
if (g_form.isNewRecord()){
g_form.removeOption('state', 2);
g_form.removeOption('state', 3);
g_form.removeOption('state', 6);
g_form.removeOption('state', 7);
g_form.removeOption('state', 8);
} else
{
g_form.addOption('state', 2);
g_form.addOption('state', 3);
g_form.addOption('state', 6);
g_form.addOption('state', 7);
g_form.addOption('state', 8);
}
}
When I run this client script I can only see the 'new' option when it's a new incident record and that's great. However, after saving it I get a few more options but some of them appear blank.
Can someone guide me into the right direction of where I am going wrong?
Thanks all 🙂
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 04:01 AM
Hi Maria,
Can you try using below syntax for addOption
g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>);
where you already have field name, you need choice value i.e. dictionary value & choice label

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 04:01 AM
Hi Maria,
Can you try using below syntax for addOption
g_form.addOption(<fieldName>, <choiceValue>, <choiceLabel>);
where you already have field name, you need choice value i.e. dictionary value & choice label
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 04:27 AM
Thanks so much Jaspal 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2023 04:11 AM
Hi @Maria da Costa If you want to only show the new option if the incident is in a "new" state, then use the following code:
function onLoad() {
if(g_form.getValue('state') == '1'){
g_form.removeOption('state',2);
g_form.removeOption('state',3);
g_form.removeOption('state',6);
g_form.removeOption('state',7);
g_form.removeOption('state',8);
}
}
If this helps you kindly, mark answer as correct
Regrads,
Pradnya