Script Include Examples and Respective Client scripts with Glide Ajax
Script Include Example 1:
This function is designed to fetch user details (employee number and location) from the sys_user table using a user's sys_id passed as a parameter.
Script Include Function:
getUserDetails: function() {
// Initialize an object to hold the result with default empty values
var result = {
emp_number: '', // Will store the employee number
emp_location: '', // Will store the location
};
// Retrieve the user sys_id passed from the client as a parameter
var user_sysID = this.getParameter('sysparm_user_sysID');
// Create a GlideRecord object to query the sys_user table
var userGR = new GlideRecord('sys_user');
// Check if a user record with the given sys_id exists
if (userGR.get(user_sysID)) {
// If found, get the employee number and location from the record
result.emp_number = userGR.getValue('employee_number');
result.emp_location = userGR.getValue('location');
}
// Convert the result object to a JSON string and return it
return JSON.stringify(result);
},
Client Script to call above script include function:
Calls a Script Include (GitHubRequests) from the client side using GlideAjax
Passes a user's sys_id to the server
Retrieves the user's employee number and location
Populates two fields on the form: emp_id and requestor_location
FYI, the below code is onChange client script to populate 2 different field values at a time.
// Create a GlideAjax object to call the Script Include named 'global.GitHubRequests'
var ga = new GlideAjax('global.GitHubRequests');
// Add the name of the function to call within the Script Include
ga.addParam('sysparm_name', 'getUserDetails');
// Pass the sys_id of the user as a parameter (usually from a field like caller or requested_for)
ga.addParam('sysparm_user_sysID', newValue);
// Send the request and handle the response asynchronously
ga.getXMLAnswer(function(response) {
// Parse the JSON string returned from the Script Include into a JavaScript object
var userDetails = JSON.parse(response);
// Set the 'emp_id' field on the form with the employee number retrieved
g_form.setValue('emp_id', userDetails.emp_number);
// Set the 'requestor_location' field on the form with the location retrieved
g_form.setValue('requestor_location', userDetails.emp_location);
});
Script Include Example 2:
This function is designed to fetch below details
Accepts a sys_id of an allocation record.
Retrieves the related cost center.
Extracts project details like code, end date, client name, and vertical.
Returns the data as a JSON string for use in client-side scripts.
Script Include Function:
getProjectDetails: function() {
// Initialize an object to hold the result with default empty values
var result = {
Project_code: '', // Will store the project code
Project_EndDate: '', // Will store the project end date
Client_Name: '', // Will store the client name
Vertical_name: '', // Will store the vertical name
};
// Retrieve the allocation record sys_id passed from the client
var allocation_sysID = this.getParameter('sysparm_allocation_record_sysID');
// Create a GlideRecord object to query the 'u_allocation' table
var allocation_GR = new GlideRecord('u_allocation');
// Check if the allocation record exists for the given sys_id
if (allocation_GR.get(allocation_sysID)) {
// Get the sys_id of the related cost center from the allocation record
var costcenter_sysID = allocation_GR.getValue('u_cost_center');
// Query the 'cmn_cost_center' table using the retrieved sys_id
var costcenter_GR = new GlideRecord('cmn_cost_center');
costcenter_GR.addQuery('sys_id', costcenter_sysID);
costcenter_GR.query();
// If a matching cost center record is found
if (costcenter_GR.next()) {
// Populate the result object with values from the cost center record
result.Project_code = costcenter_GR.getValue('code'); // Project code
result.Project_EndDate = costcenter_GR.getValue('valid_to'); // Project end date
// Get the display value of the parent cost center's name (Client Name)
result.Client_Name = costcenter_GR.parent.name.getDisplayValue();
// Get the display value of the vertical field from the parent cost center
result.Vertical_name = costcenter_GR.parent.u_vertical.getDisplayValue();
}
}
// Convert the result object to a JSON string and return it
return JSON.stringify(result);
},
Client Script to call above script include function:
Calls a Script Include (GitHubRequests) from the client side.
Passes the sys_id of an allocation record.
Retrieves project-related details from the associated cost center.
Populates form fields with the returned data.
FYI, the below code is onChange client script to populate different field values at a time.
// Create a GlideAjax object to call the Script Include named 'GitHubRequests' in the global scope
var ga = new GlideAjax('global.GitHubRequests');
// Specify the function to call within the Script Include
ga.addParam('sysparm_name', 'getProjectDetails');
// Pass the sys_id of the allocation record as a parameter
ga.addParam('sysparm_allocation_record_sysID', newValue);
// Send the request to the server and handle the response asynchronously
ga.getXMLAnswer(function(response) {
// Parse the JSON string returned from the Script Include into a JavaScript object
var projectInfo = JSON.parse(response);
// Set the 'project_code' field on the form with the value retrieved
g_form.setValue('project_code', projectInfo.Project_code);
// Set the 'project_end_date' field on the form with the value retrieved
g_form.setValue('project_end_date', projectInfo.Project_EndDate);
// Set the 'client_name' field on the form with the value retrieved
g_form.setValue('client_name', projectInfo.Client_Name);
// Set the 'vertical_name' field on the form with the value retrieved
g_form.setValue('vertical_name', projectInfo.Vertical_name);
});
Script Include Example 3:
The getCities function retrieves a list of unique city names from the cmn_location table in ServiceNow, filtered by a specific country and state. It uses GlideAggregate to group by city and returns the result as a JSON string array.
Script Include Function:
getCities: function() {
// Retrieve input parameters from the client request
var country = this.getParameter('sysparm_country_str');
var state = this.getParameter('sysparm_state_str');
// Initialize GlideAggregate to query the cmn_location table
var ga = new GlideAggregate('cmn_location');
// Apply filters to get only active locations from the "Location Master" source
ga.addQuery('cmn_location_source', 'Location Master');
ga.addQuery('u_active_yn', true); // Ensure only active records are considered
// Filter by the country and state passed from the client
ga.addQuery('country', country);
ga.addQuery('state', state);
// Ensure city field is not empty
ga.addQuery('city', '!=', '');
// Group results by city to get unique city names
ga.groupBy('city');
// Execute the query
ga.query();
// Prepare an array to hold the city names
var result = [];
// Loop through the results and collect city names
while (ga.next()) {
result.push(ga.getValue('city'));
}
// Return the result as a JSON string
return JSON.stringify(result);
},
Client Script to call above script include function:
This script runs when a field (likely state) changes. It:
Clears the existing options in the event_city choice field.
Calls a Script Include (EventManagement) to get a list of cities based on the selected country and state.
Populates the event_city field with the returned city names.
// Clear existing options and value in the 'event_city' field
g_form.clearOptions('event_city'); // Removes all dropdown options
g_form.clearValue('event_city'); // Clears the selected value
g_form.addOption('event_city', '', '-- None --'); // Adds a default placeholder option
// Create a GlideAjax call to the Script Include 'EventManagement'
var ga_state = new GlideAjax('x_gesp_event_manag.EventManagement');
// Specify the function to call within the Script Include
ga_state.addParam('sysparm_name', 'getCities');
// Pass the selected country and state as parameters
ga_state.addParam('sysparm_country_str', g_form.getValue('country'));
ga_state.addParam('sysparm_state_str', newValue); // 'newValue' is the updated state
// Handle the response from the server
ga_state.getXMLAnswer(function(response) {
var States = JSON.parse(response); // Parse the JSON string into an array
// Loop through each city and add it as an option to the 'event_city' field
States.forEach(function(city) {
g_form.addOption('event_city', city, city);
});
});