Skip to tutorial content
Rate module Not yet rated

Client-Callable Script Includes (GlideAjax)

3 min read
On this page

In this lesson, you will learn:

  • What GlideAjax is
  • How to write a client-callable script include
  • How to call it from a client script and use the answer

1 The browser needs server data

When an agent picks a Caller, SNWR wants the form to show how many open incidents that person already has. The client script knows the caller, but only the server can count incidents. GlideAjax lets the browser ask the server. πŸ“¨

Key term: GlideAjax

A client-side helper that calls a client-callable script include on the server and gets an answer back, without reloading the form.

The GlideAjax round trip. 1: in the browser, an onChange client script on Caller creates a GlideAjax for SNWRIncidentAjax with sysparm_name getOpenCount and sysparm_user_id. 2: the request goes to the server. 3: the client-callable script include reads the parameters, counts open incidents, and returns 3. 4: the answer comes back to the browser, and the callback shows a message: Riya has 3 open incidents.
Figure 1. Ask, count on the server, answer, show it.

2 The server part

Create a script include with Client callable checked (called Glide AJAX enabled in newer releases). It extends AbstractAjaxProcessor.

var SNWRIncidentAjax = Class.create();
SNWRIncidentAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  getOpenCount: function() {
    var userId = this.getParameter('sysparm_user_id');
    return new SNWRIncidentUtils().getOpenCount(userId);
  },

  type: 'SNWRIncidentAjax'
});

3 The client part

An onChange client script on Caller:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }
  var ga = new GlideAjax('SNWRIncidentAjax');     // script include name
  ga.addParam('sysparm_name', 'getOpenCount');   // method name
  ga.addParam('sysparm_user_id', newValue);      // your own parameter
  ga.getXMLAnswer(function(answer) {             // runs when the answer arrives
    g_form.showFieldMsg('caller_id',
      'This caller has ' + answer + ' open incident(s).', 'info');
  });
}
Piece What it does
sysparm_name Which method to run on the server
sysparm_... Your own parameters. Names must start with sysparm_
this.getParameter() Reads a parameter on the server
getXMLAnswer(callback) Sends the request and runs the callback with the answer

⚠️ Heads up: Use getXMLAnswer() with a callback (asynchronous). Avoid getXMLWait(). It freezes the browser while waiting, and doesn’t work in Service Portal.

πŸ“ Note: Newer releases can protect client-callable script includes with an ACL (type client_callable_script_include). If it works for you but not for other users, check that ACL.

⭐ Remember: Server: client callable + AbstractAjaxProcessor + getParameter(). Client: new GlideAjax() + sysparm_name + getXMLAnswer().

4 Let’s try it: show open incidents

  1. Create the SNWRIncidentAjax script include above. Check Client callable. Submit.
  2. Create an onChange client script on Incident, Field name Caller. Paste the client part. Submit.
  3. Open a new incident and pick a Caller. Do you see the message with a count?

5 Quick recap

  • GlideAjax lets client scripts get data from the server
  • The script include must be client callable and extend AbstractAjaxProcessor
  • sysparm_name picks the method. Other parameters start with sysparm_
  • Use getXMLAnswer() with a callback, not getXMLWait()

6 Check yourself

Think of your answer first. Then tap to check.

1 Which parameter tells GlideAjax which method to run? Show answer

sysparm_name.

2 Which class must a client-callable script include extend? Show answer

AbstractAjaxProcessor.

3 Why avoid getXMLWait()? Show answer

It freezes the browser while it waits, and it doesn’t work in Service Portal.

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.