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

Mike Allen
Mega Sage

UI Policies are onLoad.   If you want to make it read only on the change of another variable, you should use onChange Client Scripts.


gourav2
Kilo Expert

Thanks Mike. I wrote the onChange Client Script on the field but its not working. Has it got to do something with roles?


Hi Gaurav,



Can you please provide your Script here.   so that we can debug. you can also achieve that using UI policies too, for that you need to provide condition in catalog Ui policies.



Please try and let me know. i will show you that functionality in demo instance.



Thanks & Regards


Govind kumar Sharma



PS: Hit like, Helpful or Correct depending on the impact of the response.


Yes, my script is working fine now and my requirement are also filled except a few. I wanted to remove some options from one field upon selecting another field and in else condition, I wanted all the removed fields to be back. Below is my script:



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


  }  


}


---------------------------------------------------------------------------


When I select the application XYZ on my catalog item, then url field will be ReadOnly and its value will be set as N/A. In addition, the Environment field will also become ReadOnly and I will remove two extra options (Dev and Stage) while Production field will remain there, there are 3 options in Environment field.



Now, there is another field called TypeIssue which has some options and when my application field will be XYZ then those pre-existing options should be removed and the TypeIssue choice field should be fed with new entries. These all things will happen only when my Application field is XYZ.



Everything is working fine but my new fields are not visible, it seems that they are added llike empty and white spaces but I am not able to see them/ Any guesses?



Thanks a lot. | Gourav