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 ACCEPTED SOLUTION

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... 

 

View solution in original post

3 REPLIES 3

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... 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@sntrainingss 

check points mentioned from @Brad Bowman  

There are lot of GlideAjax articles which you can check and fix your code.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

RaghavSh
Kilo Patron

@sntrainingss There are few syntax mistakes in your code, refer below article which explain glideAjax with examples:

 

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

 

Also, you don't have to return object and parse the object incase you are returning single value from script include.


Raghav
MVP 2023
LinkedIn