- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 03-03-2020 12:17 PM
This article helps you get user data from the Server side on to your Client script.
1.Script Include Creation
Name: userDetailsUtil
API Name: global.userDetailsUtil (Auto populated by system)
Client Callable: Checked(True)
Active: Checked(True)
var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmployeeDetails: function() {
var userName = this.getParameter('sysparm_user');
var user = new GlideRecord('sys_user');
var result = { //Create an object to store the User data
firstName: "",
lastName: "",
userID: "",
location: "",
manager: ""
};
if (user.get(userName)) {
result.firstName = user.first_name.toString(); // toString is different from getDisplayValue
result.lastName = user.last_name.toString(); // toString is different from getDisplayValue
result.userID = user.user_name.toString(); // toString is different from getDisplayValue
result.location = user.location.getDisplayValue(); //use getDisplayValue() value for reference fields
result.manager = user.manager.getDisplayValue(); //use getDisplayValue() value for reference fields
}
return JSON.stringify(result);
},
type: 'userDetailsUtil'
});
2.Catalog Client Script creation
Name: Return User Details
Active: Checked (True)
UI Type: ALL
Type: OnChange
Variable Name: Logged in user Variable.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var user_id = g_form.getValue('requested_for');
var ga = new GlideAjax('userDetailsUtil');//name of script include
ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
ga.addParam('sysparm_user', user_id);//name of field on form triggering call
ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
}
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
g_form.setValue('first_name',result.firstName);
g_form.setValue('last_name',result.lastName);
g_form.setValue('user_name',result.userID);
g_form.setValue('location',result.location);
g_form.setValue('manager',result.manager);
}
If this article helped in you in any which way, please mark it as helpful/bookmark it.
-Best Regards
Prateek kumar
- 25,043 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for your response
Based on you provided script , i was change some variables ,all variables are executed except 'title and phone" .
would you suggest some changes for that?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
You have a space in the syntax for "number" so update that to be user.mobile_number.toString(); and it should work.
And not sure if you instance is the same as mine but Title is a choice list so you don't need to "getDisplayValue()" as a "getValue()" should be all you need.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Prateek kumar Hello your code works in service catalog but it doesn't work on service portal do you know why?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Very useful. Thanks