show the Caller details in the following format on Description First Name, Last Name, Deportment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2024 10:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2024 11:35 AM
Hi @Subhashini C ,
If you want to populate caller First Name, Last Name, Department in the description section. Then you can use getReference, g_scratchpad and GlideAjax. The best and the ServiceNow recommended way is to use GlideAjax.
I've provided you the steps and script for the particular solution:
Step 1: Create the Script Include
- Navigate to System Definition > Script Includes.
- Click on New to create a new Script Include.
- Fill in the details and add the script as follows:
Name: CallerDetails
API Name: CallerDetails
Client Callable: Checked
var CallerDetails = Class.create();
CallerDetails.prototype = {
initialize: function() {},
getCallerDetails: function(callerId) {
var result = {};
if (!callerId) {
return result;
}
var userGR = new GlideRecord('sys_user');
if (userGR.get(callerId)) {
result.firstName = userGR.getValue('first_name');
result.lastName = userGR.getValue('last_name');
var departmentId = userGR.getValue('department');
var deptGR = new GlideRecord('cmn_department');
if (deptGR.get(departmentId)) {
result.department = deptGR.getValue('name');
} else {
result.department = '';
}
}
return result;
},
type: 'CallerDetails'
};
Step 2: Create the onLoad Client Script
- Navigate to System Definition > Client Scripts.
- Click on New to create a new Client Script.
- Fill in the details and add the script as follows:
Name: Populate Caller Details in Description
Table: Incident
Type: onLoad
UI Type: All
(function() {
// Ensure the script only runs on new records
if (g_form.isNewRecord()) {
var callerId = g_form.getValue('caller_id');
if (callerId) {
// Call the Script Include to get caller details
var ga = new GlideAjax('CallerDetails');
ga.addParam('sysparm_name', 'getCallerDetails');
ga.addParam('callerId', callerId);
ga.getXMLAnswer(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var details = JSON.parse(answer);
var description = details.firstName + ' ' + details.lastName + ', ' + details.department;
g_form.setValue('description', description);
}
});
}
}
})();
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.