Populate Record producer variables with current user values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 05:25 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 05:39 AM
Hi there,
This might help:
https://www.servicenowguru.com/scripting/user-object-cheat-sheet/
For example, if it is for onLoad, you could use Default Values like:
javascript:gs.getUserID();
javascript:gs.getUser().getDepartmentID();
javascript:gs.getUser().getLocation();
etc.
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 10:47 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 05:43 AM
Hello VAishali,
You can actually write OnChange Client script to populate the values
for example :
g_form.setValue('department', g_user.getDepartmentID());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 06:08 AM
You can create a catalog client script on OnChange condition to update the user information based on the login name...somewhat similar to the following code.
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var caller = g_form.getReference('caller', popCallerInfo);
}
function popCallerInfo(caller){
g_form.setValue('contact_number', caller.phone);
g_form.setValue('location', caller.location);
g_form.setValue('requested_for_manager', caller.manager);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2019 11:13 PM
A few tips and tricks from my experience:
- Do not use OnLoad Client Script for this - it is slow, poor UX, requires more code & completely unnecessary.
Using an onLoad Client Script, you will notice that the values start off empty, then populate after a second or so. The more you do this, the slower your forms will be. - Use Default Values to populate variables for "On Load" as others have said.
Default values = instant field population, better UX. - Use the official GlideUser API documentation to help populate Default Fields. It is available here. Example usage (don't forget the 'javascript:' prefix):
javascript:gs.getUser().getMobileNumber(); //Get Mobile Number​
- If you need to get fields that are not in the documented API, use the following code
javascript:(function getUserField(fieldName) { var currentUser = gs.getUser(); var userGR = currentUser.getRecord(); return userGR.getValue(fieldName); })('email'); //Change this to the field name required​
- If there is a use case where a user is filling out this form for someone else, write OnChange scripts to handle field changes, make sure one of your variables is a Reference field of type sys_user so it is easier to retrieve user details in your on Change Client Script. Only then can you leverage the g_form.getReference API.
function onChange(control, oldValue, newValue, isLoading) { g_form.getReference('caller_id', callback); function callback(caller) { // reference is passed into callback as first arguments g_form.setValue('full_name',caller.name); }​
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022