how to make field mandatory,read only,hide using client script?

bvk
Kilo Contributor

how to make field mandatory,read only,hide using client script?

4 REPLIES 4

Priya Shekar
Giga Guru

Hi Varun,



Check the below link for reference,



http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#gsc.tab=0


find_real_file.png



Thanks,


Priyadarshini




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


Raju Koyagura
Tera Guru

Here are the methods to use in client script but I recommend not to go for client script to perform the above three actions instead use UI policies..



g_form.setMandatory("fieldName", true)


g_form.setReadOnly("fieldName", true)


g_form.setDisplay("fieldName", true) or g_form.setVisible("fieldName", true)



Ref: http://wiki.servicenow.com/index.php?title=Differences_Among_Scripts#gsc.tab=0


Hi Varun,



As per ServiceNow best practices always use UI policies/ACL's for your requirement.


If it is not achieved by above methods, use below script code for client script - onLoad



g_form.setMandatory("fieldName", true);


g_form.setReadonly("fieldName", true);


g_form.setVisible("fieldName", true);



I hope this helps 🙂


- Gopal Dutt Harbola


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


srividyagalla
Kilo Expert

HAI VARUN,



By using GlideForm (g_form) you can do this.



void setMandatory(fieldName, boolean)


Makes the field required if true.
Best Practice: Use UI Policy rather than this method whenever possible.
Parameters:
fieldName - specifies the field.
boolean - true if mandatory, false if optional.
void setReadOnly(fieldName, boolean)
Makes the field read-only if true, editable if false. To make a mandatory field read-only, you must first remove the mandatory requirement for that field by using the setMandatory method.
Note: Both setReadOnly and setReadonly are functional.
Best Practice: Use UI Policy rather than this method whenever possible.
Parameters:
fieldName - specifies the field.
boolean - true if read-only, false if editable.
void setVisible(fieldName, boolean)
Displays the field if true. Hides the field if false.
If the field is hidden, the space is left blank. This method cannot hide mandatory fields with no value.
Best Practice: Use UI Policy rather than this method whenever possible.
Parameters:
fieldName - specifies the field to be displayed or hidden.
boolean - true to display the field, false to hide the field.


referred ServiceNow Wiki


for more functions go through this link-
http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#setVisible
PS: Hit like, Helpful or Correct depending on the impact of the response, it helps the community