Skip to tutorial content
Rate module Not yet rated

Client Script Basics & g_form

3 min read
On this page

In this lesson, you will learn:

  • What a client script is
  • The key fields on a client script
  • The most useful g_form and g_user methods

1 Scripts that run on the form

UI policies handle mandatory, visible, and read only. But what if you want to set a value, show a warning, or check something before saving? That’s where client scripts come in. πŸ–₯️

Key term: Client script

JavaScript that runs in the browser on a form (or list). It reacts to the form loading, a field changing, the form being submitted, or a list cell being edited.

2 Key fields

Field What it does
Name and Table What it’s called, and which table’s form it runs on
UI Type Desktop, Mobile / Service Portal, or All
Type onLoad, onChange, onSubmit, or onCellEdit
Field name For onChange and onCellEdit: which field to watch
Inherited Also runs on tables that extend this one
Global Runs on all views. Uncheck it to pick one view

πŸ“ Note: Find client scripts in System Definition > Client Scripts. You can also right-click a form header and choose Configure > Client Scripts.

3 g_form: your form helper

The most useful g_form methods in four groups. Read: getValue, getReference, isNewRecord. Change: setValue, clearValue. Control fields: setMandatory, setDisplay, setVisible, setReadOnly. Messages: showFieldMsg, hideFieldMsg, addInfoMessage, addErrorMessage, clearMessages. Plus g_user: userName, userID, hasRole.
Figure 1. g_form reads and changes the form. g_user tells you who’s using it.
var cat = g_form.getValue('category');           // read a value
g_form.setValue('impact', '2');                  // set a value
g_form.setMandatory('cmdb_ci', true);            // make mandatory
g_form.setDisplay('subcategory', false);        // hide (no gap left)
g_form.showFieldMsg('cmdb_ci', 'Pick the router', 'info');
g_form.addErrorMessage('Something is missing!');

if (g_user.hasRole('itil')) {                    // who is the user?
  g_form.addInfoMessage('Hi, ' + g_user.firstName);
}

πŸ’‘ Good to know: setDisplay(false) hides a field and closes the gap. setVisible(false) hides it but leaves an empty space.

⚠️ Heads up: Always use the field’s column name (like cmdb_ci), not its label (like “Configuration item”).

⭐ Remember: Client script = browser. Four types: onLoad, onChange, onSubmit, onCellEdit. Use g_form and g_user.

4 Let’s try it: say hello

  1. Go to System Definition > Client Scripts and click New.
  2. Name: SNWR hello. Table: Incident. UI Type: All. Type: onLoad.
  3. Inside the function, add: g_form.addInfoMessage('Hi ' + g_user.firstName + '!'); Submit.
  4. Open any incident. Do you see your message at the top?

5 Quick recap

  • A client script runs JavaScript in the browser
  • Types: onLoad, onChange, onSubmit, onCellEdit
  • g_form reads and changes the form
  • g_user gives info about the user, like hasRole()
  • Use column names, not labels

6 Check yourself

Think of your answer first. Then tap to check.

1 Name the four client script types. Show answer

onLoad, onChange, onSubmit, and onCellEdit.

2 Which g_form method shows a message under one field? Show answer

showFieldMsg().

3 How do you check if the user has the itil role in a client script? Show answer

g_user.hasRole(‘itil’).

Nice work! You finished this lesson. πŸŽ‰

Was this lesson helpful?

Have a question or doubt?

Ask here and get help from instructors and fellow learners.

0 / 300

Only logged-in learners can post.