How to write an “On Load” client script on any INC table to make twp fields mandatory ?

Rohit Kumar5
Kilo Contributor

How to write an “On Load” client script on any INC table to make twp fields mandatory ?

1 ACCEPTED SOLUTION

Sumanth16
Kilo Patron

Hi Rohit,

 

Please go to client scripts under system definition. And click on the "New" button on the list view. Enter the name and select table as Incident and select type is onload and UI type is all. And replace field names in the below script.

 

find_real_file.png

 (or) 

 

You can do it through the UI policy also.

Please go to system definition -> Ui policies and selects new from list view 

Select the table "incident" and filter condition if applicable.

And create UI policy actions related list click on new and select those fields and make mandatory as true 

 

find_real_file.png

find_real_file.png

 

 

 

 

 

Please mark as helpful (or) correct if it fixes your issue.

 

Thanks,

Sumanth

View solution in original post

10 REPLIES 10

Dhananjay Pawar
Kilo Sage

Hi Rohit,

If you want make mandatory then go for UI policy that is the best practice.Instead of code use OOB functionality to maintain the performance.

 

Thanks,

Dhananjay.

chetanb
Tera Guru

To use UI Policy over client script is a good option when user just wants to make a field mandate.But, Genius user can open browsers developer tools (F12) and turn off the mandatory requirement. So be cautious

 

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

SN27
Giga Expert

Hi,

UI Polocy is the better option,

In client script use "g_form.setMandatory();" method.

Hit like if help ful.

Regards,

Satya.

Priyanka136
Mega Guru

Hi Rohit,

As per the Best practice:- If you want to make field Mandatory, Read only or Visible at that time always use "UI Policy" instead of "Client Script".

But in your requirement if you want to use OnLoad Client Script then replicate your code with below code:-

function onLoad() 
{
   g_form.setMandatory('your_field_name', true); // replace with your field name   
}


Let me know if you have any question !!!

Thanks,
Priyanka

Mohit Kaushik
Mega Sage
Mega Sage

Hi Rohit,

 

The best practice of making fields mandatory, read only, visible or hide we should use UI policy instead of client scripts. The reason is UI policy always override Client scripts, so if you have some UI policy on the same field then it will override the client script.

As you asked, we can achieve that with client script as well with simple code as shown below:

g_form.setMandatory('<Field Name',true);

 

And there is one more solution for this without UI policy or client script. You can simple go to form design and check the mandatory check box for the field. But this is not suggested for the extended tables. you can use this for your custom table.

 

Please mark this answer correct if it resolved the query and mark it helpful if it helped you at all

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)