Populate Caller email id,location, manager name using on change Client script

Community Alums
Not applicable

Hi,

I want to populate Caller email id,manager name,caller location in alert using on change client script on caller & glide ajax.

How can I achieve this?

Thanks in advance!

3 REPLIES 3

Community Alums
Not applicable

Hi @Community Alums 

please use following scapit to acheive the requirement.

Script include:

 

  • Go to System Definition > Script Includes in ServiceNow.
  • Create a new Script Include that retrieves the Caller’s email, Manager name, and Location based on the Caller.

Script:

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

// Method to fetch the caller's details
getCallerDetails: function() {
var callerSysId = this.getParameter('sys_id'); // Get caller's Sys ID from the client

if (!callerSysId) {
return ''; // If no caller Sys ID is provided, return an empty string
}

var userGR = new GlideRecord('sys_user');
if (userGR.get(callerSysId)) {
// Fetch caller's email, manager, and location
var email = userGR.email || '';
var manager = userGR.manager ? userGR.manager.name : ''; // Manager name
var location = userGR.location ? userGR.location.name : ''; // Location name

// Return all details in a JSON object
var details = {
email: email,
manager: manager,
location: location
};
return JSON.stringify(details); // Return the data as a JSON string
}

return ''; // Return empty if caller not found
}
});

 

Client script:

 

  • Go to System UI > Client Scripts in ServiceNow.
  • Create an On-Change Client Script on the Caller field. This will trigger every time the caller field changes.

Script: This script runs when the Caller field is changed on the Alert form
(function executeOnChange(current, previous /*null when async*/) {

// Get the Sys ID of the selected Caller (User)
var callerSysId = g_form.getValue('caller_id');

if (callerSysId) {
// Call the GlideAjax function to get the Caller details from the Script Include
var ga = new GlideAjax('CallerDetailsUtil');
ga.addParam('sys_id', callerSysId); // Pass the Sys ID of the Caller
ga.getXMLAnswer(function(response) {
var data = JSON.parse(response); // Parse the returned JSON data

// Set the fields in the Alert form
g_form.setValue('caller_email', data.email); // Set Caller Email ID
g_form.setValue('caller_manager', data.manager); // Set Manager Name
g_form.setValue('caller_location', data.location); // Set Caller Location
});
}

})(g_form.getTableName(), g_form.getUniqueValue());

 

 

 

Community Alums
Not applicable

Its showing an error in client script.

Missing function declaration for onChange
 

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

is this for a catalog item? if yes then you can use auto populate Utah feature

Auto-populate a variable based on a reference type variable (Utah) 

If this is for normal form then you can use onChange + GlideAjax

It should be an easy requirement

what did you start with and where are you stuck?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader