- 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:44 AM
Hi,
You have to use the get reference method with callback function or glideajax with script include.
Avoid GlideRecord in client scripts.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 07:51 AM
Thanks for the quick response, Sagar! I think I was trying a callback function at one point, but it wasn't working either and I deleted that script since it was a mess. I'll research more on the glideajax as well and test with that to hopefully get something working better. Again, I appreciate the quick advice on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 08:11 AM
Hi,
g_form.getReference() function with Callback method.
var userID = g_form.getReference("employees_name", getEmailID);
function getEmailID(userID) {
g_form.setValue("employee_email", userID.email);
}
You can used it for single dot walk.
For multiple dot walk use GlideAjax with Client callable Script include suggested by Chetan.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 01:20 PM
Thanks again, Sagar. This script does work, but I think Chetan is right with his response, as some additional research on GlideAjax shows that method may be the proper approach for this since I may want to fill in some additional fields that aren't always included on form submissions for us. I do appreciate you taking the time to show where I went wrong on this originally.