Incident hide fields client script

Maria da Costa
Tera Contributor

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 🙂

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

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

View solution in original post

3 REPLIES 3

Jaspal Singh
Mega Patron
Mega Patron

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

Thanks so much Jaspal 🙂

p_n
Tera Guru

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