Skip to tutorial content
Rate module Not yet rated

Client vs Server UI Actions

3 min read
On this page

In this lesson, you will learn:

  • The difference between server and client UI actions
  • How the Client checkbox and Onclick field work
  • How one UI action can run on both sides

1 Where does the click run?

So far, your UI action ran on the server. But what if you want to ask the user something first, like “Are you sure?” That has to happen in the browser. πŸ€”

Server UI action Client UI action
Client checkbox Unchecked Checked
Runs in ServiceNow server The browser
Can use current, gs, action, GlideRecord g_form, g_user, confirm()
Good for Changing data Checking the form, asking the user

2 A client UI action

Check Client. Then put a function name in Onclick, like confirmCancel(), and write that function in the Script.

function confirmCancel() {
  if (!confirm('Cancel this incident?')) {
    return false;   // user clicked Cancel: stop
  }
  g_form.setValue('state', '8');   // 8 = Canceled
  g_form.save();
}

3 Both sides in one action

Often you want a client check, then a server change. One UI action can do both!

How one UI action runs on both sides. Step 1, in the browser: the Onclick function runs, checks the form, and asks the user to confirm. Step 2: gsftSubmit sends the form to the server with the action name. Step 3, on the server: the same script runs again, and the part inside if typeof window is undefined updates current and redirects back.
Figure 1. Check in the browser, then save on the server.
// Client part (Onclick: clientCancel())
function clientCancel() {
  if (!confirm('Cancel this incident?')) {
    return false;
  }
  gsftSubmit(null, g_form.getFormElement(), 'snwr_cancel');  // Action name
}

// Server part: runs only on the server
if (typeof window == 'undefined') {
  current.state = 8;
  current.update();
  action.setRedirectURL(current);
}

πŸ“ Note: gsftSubmit() needs the UI action’s Action name. The check typeof window == 'undefined' is true only on the server, because the server has no browser window.

⚠️ Heads up: The Action name in gsftSubmit() must match exactly. A typo means the server part never runs.

⭐ Remember: Client checked + Onclick = browser. gsftSubmit() hands off to the server. typeof window == 'undefined' = server part.

4 Let’s try it: a safe Cancel button

  1. Create a UI action on Incident. Name: Cancel incident. Action name: snwr_cancel. Check Form button and Show update.
  2. Check Client. Onclick: clientCancel().
  3. Paste the “both sides” script. Submit.
  4. Open a test incident and click the button. Click Cancel in the pop-up. Nothing happens. Click it again and choose OK. Is the incident Canceled?

5 Quick recap

  • Server UI actions change data with current
  • Client UI actions use Client + Onclick and run in the browser
  • gsftSubmit() sends the form to the server with the Action name
  • typeof window == 'undefined' marks the server part

6 Check yourself

Think of your answer first. Then tap to check.

1 Which checkbox makes a UI action run in the browser? Show answer

Client.

2 Which field holds the function name for a client UI action? Show answer

Onclick.

3 What does gsftSubmit() need so the server part runs? Show answer

The UI action’s Action name.

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.