
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:05 PM
Hi Developers,
I am battling with script include, please assist by pointing out what am I doing wrong and a possible solution to the error. I have created a catalog item which I need an email address field to be auto-filled on the OnChange event of the requested for field.
Below is the UI of my catalog item:
Below is my Script Include named GetUserEmailAjaxUtils:
var GetUserEmailAjaxUtils = Class.create();
GetUserEmailAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmail: function() {
var userId = this.getParameter('sysparm_requested_for'); //value from the catalog item
if (!gs.nil(userId)){ //if userID is not empty
var requestedForUser = new GlideRecord('sys_user');
if (requestedForUser.get(userId)){
return requestedForUser.email;
}
}
return 'empty address';
}
});
Below is my catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var getEmail = new GlideAjax('GetUserEmailAjaxUtils'); //referencing the script include
getEmail.addParam('sysparm_name', 'getUserEmail'); //invoking the function in the script include
getEmail.addParam('sysparm_requested_for', g_form.getValue('requested_for')); //passing requested_for value function to the fuction
getEmail.getXML(parseResponse);
g_form.hideFieldMsg('e_mail_address');
function parseResponse(response){
var answerFromXML = response.responseXML.documentElement.getAttribute("answer");
alert(answerFromXML);
if (answerFromXML == "empty address"){
g_form.showFieldMsg('e_mail_address', 'Failed to fetch email address', 'error');
}else{
g_form.setValue('e_mail_address', answerFromXML);
}
}
}
Regards,
Kamva
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:27 PM - edited 02-12-2024 10:28 PM
Hi @Kamva ,
Intead use Autopopulate feature of ServiceNow It does not require any scripting. Open the email variable there will be a tab for auto populate. Select the requested for field there, table as user & dot walk field as email. You are done.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:08 PM - edited 02-12-2024 10:08 PM
Is the alert empty?
Does the user have email updated in the user's record?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:14 PM
Hi @Sonam_Tiwari,
The alert produces null (as shown below), and yes users have their email addresses updated in the user table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:18 PM - edited 02-12-2024 10:20 PM
Please log
userId
in your script include and check if you are getting the userid correctly from client side.
Also, better keep
//getEmail.addParam('sysparm_requested_for', g_form.getValue('requested_for'));
getEmail.addParam('sysparm_requested_for', newValue);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:26 PM - edited 02-12-2024 10:27 PM
Hi @Kamva
please try to this code.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var name = g_form.getValue('requested_for');
var user = new GlideAjax('global.script INclude Name');
user.addParam('sysparm_name', 'getuserEmail');
user.addParam('sysparm_usrsysid', name);
user.getXML(getData);
}
function getData(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(answer);
g_form.setValue('email', data.email);
}
getuserEmail: function() {
var obj = {};
var sysid = this.getParameter('sysparm_usrsysid');
var grusr = new GlideRecord("sys_user");
grusr.addQuery('sys_id', sysid);
grusr.query();
if (grusr.next()) {
obj.email = grusr.getValue('email');
}
return JSON.stringify(obj);
},
Please Mark Correct answer if it will help you.