Change choices in field depends on currently chosen value in the same field

Kasia5
Tera Contributor

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!

1 ACCEPTED SOLUTION

Rhodri
Tera Guru

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')

View solution in original post

5 REPLIES 5

Rhodri
Tera Guru

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')

Kasia5
Tera Contributor

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!

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.

Kasia5
Tera Contributor

I haven't never use switch statement, don't have experience in that..

Currently I have Client Script like below:

 

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', '5', 'To be translated', 3);
    g_form.addOption('state', '6', 'Resolved', 6);
    g_form.addOption('state', '7', 'Closed', 7);
    g_form.addOption('state', '8', 'Canceled', 8);
 
    //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');
    }
 
    if (newValue === '5') {
        g_form.removeOption('state', '1');
        g_form.removeOption('state', '7');
    }
 
    if (newValue === '6') {
        g_form.removeOption('state', '1');
    }
 
    if (newValue === '3') {
        g_form.removeOption('state', '1');
        g_form.removeOption('state', '7');
        g_form.removeOption('state', '8');
    }
    return;
}
 
And it works only when I change the value in 'state' field and only once. When i change the value for the first time it works fine, but then when I don't save but simply change once again the value then even page is not working...
 
Is it possible to make it as switch statement?