How To Pass An Array from Script Includes to Client Scripts /
Get the List of Active Incidents When the Assigned To Field Changes
This functionality retrieves and displays a list of active incidents assigned to the selected user whenever the Assigned To field is updated. It ensures that users can immediately see the workload of the selected assignee, promoting better task management and visibility.
Use Case:
When reassigning an incident to another user, the system provides immediate feedback by listing the user’s active incidents. This helps assess the workload before assigning additional tasks, improving team efficiency and resource allocation.
Check Other use case here: Return multiple values to GlideAjax from Script include
GlideAjax and Script Include: glideajax in servicenow
Client script code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } var ga = new GlideAjax("getActiveIncident"); ga.addParam("sysparm_name", "getActiveIncidents"); ga.addParam('sysparm_assigned_to', newValue); ga.getXMLAnswer(returnedInc); function returnedInc(response) { g_form.showFieldMsg('assigned_to', 'Assigned Incident: '+ response); } //Type appropriate comment here, and begin script below }
Script Include code
var getActiveIncident = Class.create(); getActiveIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, { getActiveIncidents: function() { //Retrieve active incidents var assigned = this.getParameter('sysparm_assigned_to'); var inc = new GlideRecord('incident'); inc.addActiveQuery(); inc.addQuery('assigned_to', assigned); inc.query(); var arrIncidents = []; while (inc.next()) { //Fill array arrIncidents.push(inc.number+ ' - ' + inc.getDisplayValue('state')); } return arrIncidents.toString(); }, type: 'getActiveIncident' });
Output:
Check other user case like returning a JSON Array, Objects and String: Return multiple values to GlideAjax from Script include