Make field mandatory using client script

divya123
Giga Contributor

how to make filed mandatory and read only using client script instead of using UI policies.

Regards

Divya

8 REPLIES 8

anurag92
Kilo Sage

Client script gives you an added flexibility of making a field readonly/mandatory. If you want a field mandatory on load of form, or on change of a field or on saving a form.



So the syntax would be:



g_form.setMandatory('field_to_made_mandatory',true);



same for read-only. And you can pass false to reverse the effect.



Let me know if it helps.


Harish KM
Kilo Patron
Kilo Patron

Alikutty A
Tera Sage

Hi,



You can use these functions



g_form.setMandatory('field_name', true);


g_form.setReadOnly('field_name', true);






Thanks


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


Swarup Patra
Kilo Guru

Sure, you can make a field mandatory and read-only using a client script in ServiceNow. Here's how you can do it: 1. Navigate to System Definition > Client Scripts in ServiceNow. 2. Click on New to create a new client script. 3. Fill in the necessary fields: - Name: Give a name to your client script. - Table: Select the table where you want to apply the script. - Type: Select "OnLoad" as we want the script to run when the form loads. - Script: Write the script to make the field mandatory and read-only. Here's a sample script to make a field mandatory and read-only: javascript function onLoad() { //Make a field mandatory g_form.setMandatory('field_name', true); //Make a field read-only g_form.setReadOnly('field_name', true); } Replace 'field_name' with the actual name of your field. 4. Click on Submit to save the client script. Remember, client scripts can have performance implications and should be used judiciously. UI Policies are generally a better choice for controlling field attributes as they are processed on the server side. nowKB.com