- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 08:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 08:51 AM
Hi,
Just some slight issues with syntax. Here is the correct code:
var getUserDataSI = Class.create();
getUserDataSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkrun: function(){
var userid = this.getParameter('sysparm_user_name');
var p = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userid);
gr.query();
while(gr.next()){
var q = {};
q.name = gr.name;
q.email = gr.email;
p.push(q);
}
return JSON.stringify(p);
},
type: 'getUserDataSI'
});
Just two points:
- Your return was outside of the function.
- Missing a comma at the end of the function.
Hope this helps.
Please mark this answer as helpful/solved based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 08:51 AM
Hi,
Just some slight issues with syntax. Here is the correct code:
var getUserDataSI = Class.create();
getUserDataSI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkrun: function(){
var userid = this.getParameter('sysparm_user_name');
var p = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', userid);
gr.query();
while(gr.next()){
var q = {};
q.name = gr.name;
q.email = gr.email;
p.push(q);
}
return JSON.stringify(p);
},
type: 'getUserDataSI'
});
Just two points:
- Your return was outside of the function.
- Missing a comma at the end of the function.
Hope this helps.
Please mark this answer as helpful/solved based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 09:39 AM
Hi
the correct solution has been given by
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 09:52 AM
Thanks for the help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2022 03:29 PM
FYI, the script would run but probably won't provide the correct result.
I've corrected the script by adding .toString(). Changed the class name to "UserData" and function name to "getUserInfo".
I've also change the query condition to .addQuery('user_name', userName).
var UserData = Class.create();
UserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var userName = this.getParameter('sysparm_user_name');
var grUser = new GlideRecord('sys_user');
grUser.addQuery('user_name', userName);
grUser.query();
var resultArray = [];
while (grUser.next()) {
var userDict = {};
userDict['name'] = grUser.name.toString(); // need to .toString()
userDict['email'] = grUser.email.toString(); // need to .toString()
resultArray.push(userDict);
}
return JSON.stringify(resultArray);
},
type: 'UserData '
});
If the function is to query on user's sys_id, there would be only one match so there isn't a need to return an array with just one element in it. In this case, following script will return the user info of matching sys_id.
var UserData = Class.create();
UserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var userSysId = this.getParameter('sysparm_user_name');
var grUser = new GlideRecord('sys_user');
if (grUser.get(userSysId)) {
var userDict = {};
userDict['name'] = grUser.name.toString();
userDict['email'] = grUser.email.toString();
return JSON.stringify(userDict);
}
},
type: 'UserData '
});
