How to set a catalog item variable to ReadOnly?

gourav2
Kilo Expert

There is one Catalog Item and when I select a particular field or variable then some   another variable should become ReadOnly or I can set some values in it. Which one will be the best way, an onChange Client Script or UI Policy?

1 ACCEPTED SOLUTION

Hi Gourav,



addOption() needs atleast three parameters-


1st - field name


2nd - Choice Value to be added


3rd - Choice Label of the choice value to be added


4th(optional) - index of the choice to be added in the drop down



You are missing labels of the added choices so you were not getting any names for the added options and were shown blank.



Try this code-



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


    if(newValue == 'XYZ')


    {


      g_form.setReadOnly('url', true);


      g_form.setValue('url', 'N/A');


      g_form.setValue('trans_sb_Environment', 'Production');


      g_form.setReadOnly('Environment', true);                   //If you are setting it to read only then why are you removing options from it


      g_form.removeOption('Environment', 'Dev');         // You just need to set this field to required option. This and next line is not required


      g_form.removeOption('Environment', 'Stage');         //As its read only user will not be able to select any other value


      g_form.removeOption('TypeIssue', 'Administration Module');


      g_form.addOption('TypeIssue', 'Access Request','Access Request');                 //If you want to remove all options use clearOptions() instead


      //g_form.setDisplay('Access Request', true);                   You don't need this line as it will always be displayed until you remove it


      g_form.addOption('TypeIssue', 'Request','Request');


      //g_form.setDisplay(' Request', true);         You don't need this line as it will always be displayed until you remove it


    }


  else


  {


        g_form.setReadOnly('url', false);


        g_form.setValue('url', '');


        g_form.setReadOnly('Environment', false);


  g_form.addOption('Environment', 'Dev','Dev');


//   g_form.setDisplay('Dev', true);         You don't need this line as it will always be displayed until you remove it


    g_form.addOption('Environment', 'Stage','Stage');


// g_form.setDisplay('Stage', true);         You don't need this line as it will always be displayed until you remove it


  }


}



I have also mentioned few suggestions in the comments. Please apply which are applicable to you.



For more help check below links-


GlideForm (g form) - ServiceNow Wiki


Removing or Disabling Choice List Options - ServiceNow Guru



Thanks,


Tanaji Patil



--Mark correct/helpful if it helps solving your issue.


View solution in original post

8 REPLIES 8

Shouldn't you be displaying the TypeIssue field, and not the options you've added?



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


    if(newValue == 'XYZ')


    {


      g_form.setReadOnly('url', true);


      g_form.setValue('url', 'N/A');


      g_form.setValue('trans_sb_Environment', 'Production');


      g_form.setReadOnly('Environment', true);


      g_form.removeOption('Environment', 'Dev');


      g_form.removeOption('Environment', 'Stage');


      g_form.removeOption('TypeIssue', 'Administration Module');


      g_form.addOption('TypeIssue', 'Access Request');


      g_form.setDisplay('Access Request', true);         <-------------------------


      g_form.addOption('TypeIssue', 'Request');


      g_form.setDisplay(' Request', true);       <-------------------------


    }


  else


  {


        g_form.setReadOnly('url', false);


        g_form.setValue('url', '');


        g_form.setReadOnly('Environment', false);


  g_form.addOption('Environment', 'Dev');


g_form.setDisplay('Dev', true);                   <--------------------


    g_form.addOption('Environment', 'Stage');


  g_form.setDisplay('Stage', true);                   <--------------------


  }


}


Hi Gourav,



addOption() needs atleast three parameters-


1st - field name


2nd - Choice Value to be added


3rd - Choice Label of the choice value to be added


4th(optional) - index of the choice to be added in the drop down



You are missing labels of the added choices so you were not getting any names for the added options and were shown blank.



Try this code-



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


    if(newValue == 'XYZ')


    {


      g_form.setReadOnly('url', true);


      g_form.setValue('url', 'N/A');


      g_form.setValue('trans_sb_Environment', 'Production');


      g_form.setReadOnly('Environment', true);                   //If you are setting it to read only then why are you removing options from it


      g_form.removeOption('Environment', 'Dev');         // You just need to set this field to required option. This and next line is not required


      g_form.removeOption('Environment', 'Stage');         //As its read only user will not be able to select any other value


      g_form.removeOption('TypeIssue', 'Administration Module');


      g_form.addOption('TypeIssue', 'Access Request','Access Request');                 //If you want to remove all options use clearOptions() instead


      //g_form.setDisplay('Access Request', true);                   You don't need this line as it will always be displayed until you remove it


      g_form.addOption('TypeIssue', 'Request','Request');


      //g_form.setDisplay(' Request', true);         You don't need this line as it will always be displayed until you remove it


    }


  else


  {


        g_form.setReadOnly('url', false);


        g_form.setValue('url', '');


        g_form.setReadOnly('Environment', false);


  g_form.addOption('Environment', 'Dev','Dev');


//   g_form.setDisplay('Dev', true);         You don't need this line as it will always be displayed until you remove it


    g_form.addOption('Environment', 'Stage','Stage');


// g_form.setDisplay('Stage', true);         You don't need this line as it will always be displayed until you remove it


  }


}



I have also mentioned few suggestions in the comments. Please apply which are applicable to you.



For more help check below links-


GlideForm (g form) - ServiceNow Wiki


Removing or Disabling Choice List Options - ServiceNow Guru



Thanks,


Tanaji Patil



--Mark correct/helpful if it helps solving your issue.


And also for your information-


Usually UI policies works onChange as soon as the conditions are met.


Also they can be made to run onLoad by checking onLoad check box in advance view.(by default its checked).



You can use both,UI policy or Client script, to achieve your requirement.



I always prefer UI policies (instead of onChange or onLoad client scripts) because-


1. its easier to configure as it has condition builder and policy actions (with no script required).


2. You can set order in UI policies.


3. You can also write script in advanced view.


4. Unlike client scripts code runs only when relative conditions are met.



There is only one thing we have to keep in mind with UI policies is that UI policy will not run agian if you already have met the condition.


For example,       for condition1 you want action1


                                                for condition2 you want action2



If you check both these, condition1 and condition2 in a UI policy and try to run respective actions, action1 and action2, in the script after checking the condition met in the script this will not give you full result.


Suppose condition1 is satisfied. This will run action1. But now if condition2 is met immediately then the UI policy will not run again as its already met and action2 will not be applied.



But you can write 2 separate UI policies for both the conditions. Problem is when the number of such conditions(with different action for each condition) are more. You need one UI policy for each such condition.



--Mark correct/helpful if it helps solving your issue.


Working perfectly fine guys, thank you very much.