- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2025 02:22 PM
Hello Team,
I am trying to put the FirstName and LastName in Short Description when the Caller changes.
Script Include:
On Change Client Script:
Best Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 06:18 AM
Hi @sayanghosh0 ,
Suggested Solution:
Your current issue is caused by how you are handling the response from the Script Include. Since the Script Include returns an array (even if it contains just one object), you need to access the first index [0] of the array to retrieve the caller's information.
Here’s a step-by-step guide to resolve the issue:
Update Your Client Script
Modify the client script to properly handle the array returned from the Script Include:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var glideAjax = new GlideAjax('IncidentUtils');
glideAjax.addParam('sysparm_name', 'callerInfo');
glideAjax.addParam('sysparm_caller', newValue);
glideAjax.getXMLAnswer(pop);
function pop(response) {
if (response) {
var answer = JSON.parse(response);
alert('answer' + answer);
var sd = 'First Name : ' + answer[0].firstName + ' Last Name : ' + answer[0].lastName;
g_form.setValue('short_description', sd);
} else {
alert('error');
}
}
}
Understanding the Script Include
Since your Script Include is returning an array, even if it contains only one object, ensure the client script processes the array correctly.Recommendation
In my opinion, there is no need to return an array from the Script Include when you are dealing with just one object. Instead, you can directly return the object.
var IncidentUtils = Class.create();
IncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
callerInfo: function () {
var callerSysId = this.getParameter('sysparm_caller');
var userRecord = new GlideRecord('sys_user');
userRecord.addQuery('sys_id', callerSysId);
userRecord.query();
if (userRecord.next()) {
var userObj = {
firstName: userRecord.first_name.toString(),
lastName: userRecord.last_name.toString()
};
return JSON.stringify(userObj); // Return an object instead of an array
}
return null;
},
type: 'IncidentUtils'
});
If this answer helps you solve your query, please mark it as accepted and helpful.
Best Regards,
Siddhesh Jadhav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 06:18 AM
Hi @sayanghosh0 ,
Suggested Solution:
Your current issue is caused by how you are handling the response from the Script Include. Since the Script Include returns an array (even if it contains just one object), you need to access the first index [0] of the array to retrieve the caller's information.
Here’s a step-by-step guide to resolve the issue:
Update Your Client Script
Modify the client script to properly handle the array returned from the Script Include:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var glideAjax = new GlideAjax('IncidentUtils');
glideAjax.addParam('sysparm_name', 'callerInfo');
glideAjax.addParam('sysparm_caller', newValue);
glideAjax.getXMLAnswer(pop);
function pop(response) {
if (response) {
var answer = JSON.parse(response);
alert('answer' + answer);
var sd = 'First Name : ' + answer[0].firstName + ' Last Name : ' + answer[0].lastName;
g_form.setValue('short_description', sd);
} else {
alert('error');
}
}
}
Understanding the Script Include
Since your Script Include is returning an array, even if it contains only one object, ensure the client script processes the array correctly.Recommendation
In my opinion, there is no need to return an array from the Script Include when you are dealing with just one object. Instead, you can directly return the object.
var IncidentUtils = Class.create();
IncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
callerInfo: function () {
var callerSysId = this.getParameter('sysparm_caller');
var userRecord = new GlideRecord('sys_user');
userRecord.addQuery('sys_id', callerSysId);
userRecord.query();
if (userRecord.next()) {
var userObj = {
firstName: userRecord.first_name.toString(),
lastName: userRecord.last_name.toString()
};
return JSON.stringify(userObj); // Return an object instead of an array
}
return null;
},
type: 'IncidentUtils'
});
If this answer helps you solve your query, please mark it as accepted and helpful.
Best Regards,
Siddhesh Jadhav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 08:53 AM
Thanks , it works!! 😃
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 06:43 AM
no need of GlideAjax here
you can also use getReference() with callback method
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if(newValue == ''){
g_form.clearValue('short_description');
}
var ref = g_form.getReference('caller_id', callBackMethod);
}
function callBackMethod(ref){
if(ref.first_name && ref.last_name)
g_form.setValue('short_description', 'First name is ' + ref.first_name + ' Last name is ' + ref.last_name);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader