Make field mandatory using client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2017 01:25 AM
how to make filed mandatory and read only using client script instead of using UI policies.
Regards
Divya
- 32,871 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2017 01:29 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2017 01:29 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2017 01:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-18-2024 01:38 AM
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