GlideAjax - Client Callable Script Include
Table of Contents
Introduction
In this tutorial, we will explore how to create and utilize GlideAjax in ServiceNow to enable client-side calls to server-side scripts. This guide builds upon previous concepts of Script Includes and focuses on extending existing classes and leveraging the GlideAjax API for efficient data retrieval. Whether you are preparing for a ServiceNow Developer role or enhancing your skills, this tutorial will provide you with practical insights and hands-on examples.
Chapter 1: Extend an Existing Class
To extend an existing class in ServiceNow, follow these steps:
-
Create a New Script Include:
- In the ServiceNow Studio, create a new Script Include.
- Ensure to check the "Client Callable" checkbox, which automatically extends the
AbstractAjaxProcessor
class.
-
Define the Class:
- Use the following structure to define your new class:
var Test = Class.create(); Test.prototype = Object.extendsObject(AbstractAjaxProcessor, { // Your methods will go here });
-
Access Methods from the Extended Class:
- You can now access methods defined in both your new Script Include and the extended
AbstractAjaxProcessor
class.
- You can now access methods defined in both your new Script Include and the extended
Chapter 2: Client Callable Script Include
This section focuses on creating a Script Include that can be called from client-side scripts.
-
Define the Client Callable Script Include:
- In your Script Include, define a function that will be callable from the client-side:
getEmailID: function() { // Logic to retrieve email }
-
Set Up the Client Script:
- Create a new Client Script and define it as 'onChange' of a specific field (e.g., color).
- Implement the following steps in the Client Script:
function onChange(control, oldValue, newValue, isLoading) { var ga = new GlideAjax('YourScriptIncludeName'); ga.addParam('sysparm_name', 'getEmailID'); // Specify the method name ga.addParam('sysparm_user_id', g_form.getValue('caller_id')); // Pass parameters ga.getXMLAnswer(function(response) { var emailID = response; // Process the response g_form.setValue('email', emailID); // Set the email field }); }
Chapter 3: Using GlideAjax
GlideAjax is a powerful client-side API that facilitates the calling of Script Includes.
-
Define GlideAjax:
- Create an instance of GlideAjax in your Client Script by specifying the Script Include name.
- Use the
addParam
method to specify the method and parameters you want to pass.
-
Process the Response:
- Use the
getXMLAnswer
method to send the request and handle the response asynchronously. - Access the returned data using a callback function.
- Use the
-
Example of XML Response:
- The XML response returned from the Script Include typically looks like this:
<response> <answer>example@example.com</answer> </response>
- To unwrap the XML and access the desired value, use:
var emailID = response.responseXML.documentElement.getAttribute('answer');
Chapter 4: Use Case Implementation
Let’s implement a use case where changing a field (like color) updates an email field dynamically.
-
Create the Script Include:
- Define a method to retrieve the email based on the user ID.
getEmailID: function() { var userID = this.getParameter('sysparm_user_id'); var gr = new GlideRecord('sys_user'); gr.get(userID); return gr.email; // Return the email address }
-
Set Up the Client Script:
- In the client script, implement the logic to call the Script Include when the color field is changed.
- Ensure to pass the necessary parameters as shown in previous sections.
-
Testing:
- Test the implementation by changing the color field and observing if the email field updates accordingly.
Conclusion
In this tutorial, we successfully learned how to create a Client Callable Script Include, extend existing classes, and utilize the GlideAjax API to facilitate client-server communication in ServiceNow. This approach not only enhances the efficiency of data retrieval but also aligns with best practices recommended by ServiceNow. For further learning, consider exploring advanced topics such as returning multiple values from Script Includes and handling complex data structures.