I am not sure why script include is not working

sntrainingss
Tera Contributor
To get email based on the caller field.
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {

var name = g_form.getValue('caller_id');

var gh = new GlideAjax('getEmail');
gh.addParam('sysparm_user', 'getEmail');
gh.addParam('sysparm_user_name', name);
alert(name);
gh.getXML(getDetails);

function getDetails(response) {
//var pa = JSON.parse(response);
alert('entered function');
var answer = (response.responseXML.documentElement.getAttribute("answer"));
g_form.setValue('u_email', answer);
alert(u_email);
}

}
 
var getEmail = Class.create();
getEmail.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmail : function() {

gs.log('this is called');
var peru = this.getParameter('name');
var ga = new GlideRecord('sys_user');
ga.addQuery('sys_id', peru);
ga.query();
if (ga.next()) {
var email = ga.getValue('email');
}
return email;


},

type: 'getEmail'
});
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

The line which specifies the Function name within the script include MUST be adding the parameter sysparm_name, and you are asking for trouble naming the Script Include and the Function the same name. Also, the getParameter in the Script Include has to use the same value as the addParam in the Client Script. 

 

1) change the name of the Script Include and the Glide Ajax line,

2) the first addParam should look more like this:

gh.addParam('sysparm_name', 'getEmail');

In the Script Include, the getParameter line needs to agree, so

var peru = this.getParameter('sysparm_user_name');

 

These changes will get you closer.  There may still be a syntax or logic error, but you have the logs to help spot that.  Here's an excellent guide on how Glide Ajax works.  The example returns more than one value, which you will need sooner or later.

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...