
- 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:38 PM
- 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:36 PM
If you are on or above Utah, do what @Danish Bhairag2 has suggested.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 10:45 PM
@Kamva This can be done without scripting. You should use "Auto populate" feature available in variable . Open the "Email" variable and go to "Auto populate" section.
In dependent question, select "Requested for" and then select "Email" in Dot walk path.
Go for OOB solutions instead of scripting and its very easy configure.
I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!