🔍 g_form
What is g_form?
g_form is a JavaScript object used in *Client Scripts* to control form behavior, field visibility, values, labels, and more – in real-time!
🧰Commonly usedg_form
Methods:
g_form.setValue(fieldName, value); //👉 Sets the value of a field dynamically.
g_form.getValue(fieldName); //👉 Retrieves the current value of a form field.
g_form.setVisible(fieldName, true/false); //👉 Show or hide a field on the form.
g_form.setMandatory(fieldName, true/false); //👉 Make a field mandatory or optional.
g_form.addInfoMessage('message'); //👉 Display an info message on top of the form.
g_form.clearValue(fieldName); //👉 Clears a field’s current value.
g_form.setReadOnly(fieldName, true/false); //👉 Make a field read-only or editable.
🧠 Top 10 Interview Questions & Answers
1️⃣ What is g_form in ServiceNow?
g_form is used in client-side scripts to interact with form fields, such as changing visibility, values, and states dynamically.
2️⃣ How do you set a value in a field using g_form?
g_form.setValue('priority', '1');
3️⃣ How to hide a field on form load?
Use form.setVisible('field_name', false); in an onLoad script.
4️⃣ Difference between getValue and getDisplayValue?
getValue returns the sys\_id; getDisplayValue returns the human-readable value.
5️⃣ What is the use of g_form.setMandatory()?
It dynamically makes a field required or not.
6️⃣ How do you show an info message to the user?
Use g_form.addInfoMessage('Message here');
7️⃣ Can you disable a field based on user role?
Yes, check user roles and use setReadOnly.
8️⃣ What happens if g_form.setValue() is used on a reference field?
It sets the sys\_id of the referenced record.
9️⃣ Can g_form be used in UI Policies?
Yes, for scripting in advanced UI policy actions.
🔟How to clear a reference field using g\_form?
Use g_form.clearValue('reference_field');