- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 10:28 AM - edited 11-22-2023 02:09 AM
Hello friends,
I am trying to fetch logged in user information in onLoad() client script.
When the user logs in and opens, I want to fetch some of the info from sys_user table and based on that I want to display some fields.
Since we cannot use g_scratchpad object I tried with glideAjax. But I want to pass on logged in user's UserID as a parameter in GlideAjax to script include (code in RED color). I am not able to do so. Please correct me.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 06:16 PM
@Asmita7 Here is the updated code you should you to fix the issue you are facing.
Client Script:
function onLoad() {
var userid = g_user.userID;
var ga = new GlideAjax('glideAjax_costsplit');
ga.addParam('sysparm_name', 'getUserData');
ga.addParam('sysparm_user_name', g_user.userID); //use g_user.userID for sys_id and g_user.userName to get user name
ga.getXML(scriptResponse);
function scriptResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(res);
}
}
Script Include:
var glideAjax_Account = Class.create();
glideAjax_Account.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserData: function () {
var rec = new GlideRecord('sys_user');
rec.addQuery('manager', this.getParameter('sysparm_user_name'));
rec.query();
//return "Demo";
return "Demo " + rec.getRowCount();
},
type: 'glideAjax_Account'
});
Your previous code was crashing due to the following lines
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(answer);
Here you are assigning the value to res variables, but using g_form.addInfoMessage(answer) to show the value on form where answer variable doesn't even exist.
Please mark this answer helpful and correct if it manages to address your issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 07:33 PM
HI @Asmita7 ,
I trust you are doing great.
Please find the below code for the same.
Client script :
function onLoad() {
var userid = g_user.userID;
var ga = new GlideAjax('glideAjax_Account');
ga.addParam('sysparm_name', 'getUserData');
ga.addParam('sysparm_user_name', userid); // Correctly passing the userID
ga.getXML(scriptResponse);
function scriptResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(res); // Corrected to use 'res' instead of 'answer'
}
}
Script include :
var glideAjax_Account = Class.create();
glideAjax_Account.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserData: function () {
var rec = new GlideRecord('sys_user');
rec.addQuery('sys_id', this.getParameter('sysparm_user_name')); // Changed to sys_id for userID
rec.query();
return "Demo " + rec.getRowCount();
},
type: 'glideAjax_Account'
});
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 07:47 PM
Hi @Asmita7
You can also get the current user sys_id in from the Ajax Script Include.
function onLoad() {
var ga = new GlideAjax('glideAjax_Account');
ga.addParam('sysparm_name', 'getUserData');
ga.getXMLAnswer(function(response){
g_form.addInfoMessage(response);
});
}
var glideAjax_Account = Class.create();
glideAjax_Account.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserData: function () {
var rec = new GlideRecord('sys_user');
rec.addQuery('manager', gs.getUserID());
rec.query();
//return "Demo";
return "Demo " + rec.getRowCount();
},
type: 'glideAjax_Account'
});
Cheers,
Tai Vu

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 10:32 AM
@Asmita7 Please update your script as follows.
function onLoad() {
var userid = g_user.userID;
var ga = new GlideAjax('glideAjax_costsplit');
ga.addParam('sysparm_name', 'getUserData');
ga.addParam('sysparm_user_name', g_user.userName); //use g_user.userID for sys_id and g_user.userName to get user name
ga.getXML(scriptResponse);
function scriptResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(answer);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 05:19 PM
Thank you @Sandeep Rajput for time.
I have already tried with userName and other parameters but no luck. I have updated the question accordingly and pasted the Script Include as well.
Please suggest.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 06:16 PM
@Asmita7 Here is the updated code you should you to fix the issue you are facing.
Client Script:
function onLoad() {
var userid = g_user.userID;
var ga = new GlideAjax('glideAjax_costsplit');
ga.addParam('sysparm_name', 'getUserData');
ga.addParam('sysparm_user_name', g_user.userID); //use g_user.userID for sys_id and g_user.userName to get user name
ga.getXML(scriptResponse);
function scriptResponse(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(res);
}
}
Script Include:
var glideAjax_Account = Class.create();
glideAjax_Account.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserData: function () {
var rec = new GlideRecord('sys_user');
rec.addQuery('manager', this.getParameter('sysparm_user_name'));
rec.query();
//return "Demo";
return "Demo " + rec.getRowCount();
},
type: 'glideAjax_Account'
});
Your previous code was crashing due to the following lines
var res = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage(answer);
Here you are assigning the value to res variables, but using g_form.addInfoMessage(answer) to show the value on form where answer variable doesn't even exist.
Please mark this answer helpful and correct if it manages to address your issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2023 02:04 AM
Thank you for rectifying the error.