- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 07:41 AM
Hello all,
I've been researching and testing a recent request for our instance, and I seem to be running into a wall. Also, I'm just starting to learn the ServiceNow scripting side, so please forgive my ignorance if I'm not approaching this correctly, or if there's something I'm missing from a conceptual standpoint.
At my organization, we have a catalog item that needs to be updated to include a new variable field to capture the e-mail address of the employee noted on a different variable field. The issue I'm running into is when I attempt to run my query, nothing is being returned to assign to the designated variable field.
If anyone is willing to offer any advice on how to best approach this, I'd greatly appreciate any suggestions.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//Type appropriate comment here, and begin script below
var emp = g_form.getValue('employees_name');
var empGR = new GlideRecord('sys_user');
empGR.addQuery('sys_id',emp);
empGR.query();
if (empGR.next())
{
gform.setValue('employee_email', empGR.email);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 07:52 AM
Hi Saddington,
foremost using glide record in client script is not good practice, you have to use GlideAjax and Script Include for server-side code as below
Client Script :-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var emp = g_form.getValue('employees_name'); // onchnage emp value below script will get run make sure empployee name is refernce field and refer to sys_user
var glideAjax = new GlideAjax('MyFavoritesAjax'); // client callbale script include name
glideAjax.addParam('sysparm_name', 'get_email'); // function name
glideAjax.addParam('sysparm_user', emp ); // parameter to pass on script include
glideAjax.getXML(getResults);
}
function getResults(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('employee_email', answer);
}
Script Include :(client callable true)
var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
get_manager: function() {
var empID = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', empID);
gr.query();
if (gr.next()) {
return gr.email.getDisplayValue(); // or use return gr.email.toString(); it will return email to client script as answer
}
},
type: "MyFavoritesAjax"
});
Let me know if you get issue in this script
Kindly mark correct and helpful if applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 07:52 AM
Hi Saddington,
foremost using glide record in client script is not good practice, you have to use GlideAjax and Script Include for server-side code as below
Client Script :-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var emp = g_form.getValue('employees_name'); // onchnage emp value below script will get run make sure empployee name is refernce field and refer to sys_user
var glideAjax = new GlideAjax('MyFavoritesAjax'); // client callbale script include name
glideAjax.addParam('sysparm_name', 'get_email'); // function name
glideAjax.addParam('sysparm_user', emp ); // parameter to pass on script include
glideAjax.getXML(getResults);
}
function getResults(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('employee_email', answer);
}
Script Include :(client callable true)
var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
get_manager: function() {
var empID = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', empID);
gr.query();
if (gr.next()) {
return gr.email.getDisplayValue(); // or use return gr.email.toString(); it will return email to client script as answer
}
},
type: "MyFavoritesAjax"
});
Let me know if you get issue in this script
Kindly mark correct and helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 08:22 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var volume = g_form.getReference("employees_name", setCurrentValues);
function setCurrentValues(volume) {
var email = volume.email;
// var hostname = g_form.getValue('csv_server_hostname');
g_form.setValue('employees_email', email);
}
}
Try this if the employee_name is the reference field, and also please check the names
employees_name and employee_email
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 01:22 PM
Chetan,
Thanks for the detailed answer on this. I think this would be the proper approach on this in my case, especially for future use as well, so I've marked this as the correct answer. Thanks again for the advice, as I'll be sure to keep that in mind for future reference as I do more scripting within the platform.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2022 08:32 AM
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var volume = g_form.getReference("employees_name", setCurrentValues);
function setCurrentValues(volume) {
var email = volume.email;
// var hostname = g_form.getValue('csv_server_hostname');
g_form.setValue('employees_email', email);
}
}
Try this if the employee_name is the reference field, and also please check the names
employees_name and employee_email