Returning values from script include in client script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 02:00 PM
My script include is getting values from the 'sys_user' table and 'cmdb_ci_computer' table. However the values are not returning (and not returning in my log statements).
I'm just trying to return one of the functions form my script include in my client script, but am getting a js console error: 'Unhandled exception in GlideAjax.' - old_function(text);
Need help to troubleshoot it. My client script is:
function onChange(control, oldValue, newValue, isLoading) {
//call script include to return user fields
var ga = new GlideAjax('ITSMws1Fields');
ga.addParam('sysparm_name','getPosition');
ga.addParam("sysparm_user", newValue);
ga.getXMLAnswer(setField);
}
//set position and location field values
function setField(response) {
var answer = response;
if (answer != '') {
g_form.setValue('position_title', position);
}
}
My script include set to client callable and is:
var ITSMws1Fields = Class.create();
ITSMws1Fields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getPosition: function() {
var user_id = this.getParameter('sysparm_user');
var position = '';
var gr = new GlideRecord('sys_user');
gr.addQuery('title');
gr.query();
if(gr_user.get(user_id)) {
position = title; }
gs.log('postion');
return position;
},
getLocation: function() {
var user_id = this.getParameter('sysparm_user');
var work_location = '';
var gr = new GlideRecord('sys_user');
gr.addQuery('location');
gr.query();
if(gr_user.get(user_id)) {
work_location = location; }
gs.log('location');
return location;
},
getComputer: function(user_id) {
var ans = '';
var gr_computer = new GlideRecord('cmdb_ci_computer');
if(gr_computer.get('assigned_to', user_id))
ans = gr_computer.getUniqueValue();
gs.log('ans');
return ans;
},
type: 'ITSMws1Fields'
});
cheers
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 03:54 PM
your GlideRecord scripts are not correct. you need to use the same format that i used.
for example
if(gr_computer.get.... will not work
you should use
if(gr.next()){
}
or
while(gr.next()){
}
use the following code
getLocation: function(){
var work_location = '';
var user_id = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.get(user_id);
work_location = gr.getDisplayValue('location');
return work_location;
},
getComputer: function(){
var computer = '';
var user_id = this.getParameter('sysparm_user');
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('assigned_to',user_id);
gr.query;
if(gr.next()){
computer = gr.getDisplayValue('name');
return computer;
},
this link will help you with GlideRecords in the future
https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 07:23 PM
Thanks I'm still not returning any values, so will have a look at the GlideRecord cheat sheet and hopefully figure it out.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 03:58 PM
also gs.log('computer'); will return the string value of computer and not the variable value because you have put it in 'quotes'
you should be using gs.log(computer); to return the variable value

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 08:01 PM
Can you please try the below:
getPosition: function() {
var user_id = this.getParameter('sysparm_user');
var position = '';
var gr = new GlideRecord('sys_user');
gr.addQuery('user_name', user_id);
gr.query();
if(gr.next()){
position = gr.getValue("title");
}
gs.log('position ' + position);
return position;
},
Note: Please check the title exist for that particular user in user table.
Let me know if it helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2019 08:26 PM
Thanks - it turns out we don't use the sys_user.title field. It is populated from the 'sn_hr_core_profile' and sits in our 'hr_position' table. hr_position.position.
I'll try to rework it.