How to auto populate the logged in user details using onload client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2022 01:43 AM
Hi All,
I have a requirement where I have created multiple variables - RequestedFor, RequestedBy, Location, City, Country, MobileNumber, ManagerName and incidentLists
1.user opens the form above details auto populate with logged in user
2.on change of user, above details to be changed auto change based on the user change
3.incidenlists var only be visible on the form once the all the user details entered
4.populate all the list of incidents raised by selcted user - reqfor
Please help me to achieve this requirement
Thanks&Regards,
Sweety.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2022 02:43 AM
Yes Ankur,
When the form loads I have auto populate all details of the user.
Regards
Sweety
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2022 03:47 AM
Hi,
you can populate the default value in variables
location reference variable
javascript: gs.getUser().getLocation();
country string variable
javascript: gs.getUser().getRecord().getValue('country');
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 05:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 08:37 PM
update as this
Script Include:
var getDetails = Class.create();
getDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var user = this.getParameter('sysparm_userselect');
var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()) {
var umobile = gr.getDisplayValue('mobile_phone');
var uemail = gr.getDisplayValue('email');
var umanager = gr.getDisplayValue('manager');
var ulocation = gr.getDisplayValue('location');
var ucity = gr.getDisplayValue('city');
var ucountry = gr.getDisplayValue('country');
}
var obj = {
phone: umobile,
email: uemail,
manager: umanager,
location: ulocation,
city: ucity,
country: ucountry
};
return JSON.stringyfy(obj);
},
type: 'getDetails'
});
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 05:56 AM
Hey,
For 3rd acceptance criteria, please use UI policy with condition, if requested for value is populated, visible is true for incident list(Preferred way)
You will need onChange and onLoad both, scripts will remain more or less the same for both, in case of onload you just to pass uservalue instead of newValue and in onChange you need to pass newValue(considering you have built onchange on the userselected field)
var obj= {};
if (gr.next()) {
obj.umobile = gr.getDisplayValue('mobile_phone');
obj.uemail = gr.getDisplayValue('email');
obj.umanager = gr.getDisplayValue('manager');
obj.ulocation = gr.getClassDisplayValue('location'); // are you sure of this function
obj.ucity = gr.getDisplayValue('city');
obj.ucountry = gr.getDisplayValue('country');
}
gs.info("test json = "+JSON.stringify(obj)); // just to verify if you are getting right object
return JSON.stringify(obj);
}
Aman Kumar
Hi Ankur,
I have writen below code, but it's not working. If there any changes in the script please let me know
onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue) {
g_form.clearValue('phone_number');
g_form.clearValue('email_id');
g_form.clearValue('manager_name');
g_form.clearValue('location');
g_form.clearValue('city');
g_form.clearValue('country');
// g_form.clearValue('incidents_list');
}
var userselected = g_form.getValue('newValue');
var ga = new GlideAjax('getDetails');
ga.addParam('sysparm_name', 'getUserDetails');
// ga.addParam('sysparm_userselect', newValue);
ga.addParam('sysparm_userselect', userselected);
ga.getXML(UserDetails);
// }
function UserDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var obj = JSON.parse(answer);
g_form.setValue('phone_number', obj.phone);
g_form.setValue('email_id',obj.email);
g_form.setValue('manager_name', obj.manager);
g_form.setValue('location', obj.location);
g_form.setValue('city', obj.city);
g_form.setValue('country', obj.country);
// g_form.setValue('incidents_list', obj.incident);
}
}
Script include
var getDetails = Class.create();
getDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var user = this.getParameter('sysparm_userselect');
var gr = new GlideRecord('sys_user');
gr.addQuery("sys_id", user);
gr.query();
if (gr.next()) {
var umobile = gr.getDisplayValue('mobile_phone');
var uemail = gr.getDisplayValue('email');
var umanager = gr.getDisplayValue('manager');
var ulocation = gr.getClassDisplayValue('location');
var ucity = gr.getDisplayValue('city');
var ucountry = gr.getDisplayValue('country');
}
var json = new global.JSON();
var obj = {
phone: umobile,
email: uemail,
manager: umanager,
location: ulocation,
city: ucity,
country: ucountry
};
return (JSON.stringyfy(obj));
},
type: 'getDetails'
});
Regards,
Sweety