Populate Record producer variables with current user values

vaishali mahal1
Tera Contributor

Hello team,

i have record producer and i am able retrieve only userID.like this.

find_real_file.png

I have User[sys_user] table having details regarding this logged in user but i am not sure to retrieve that values and populate in record producer.

User[sys_user] will like this,

find_real_file.png

10 REPLIES 10

Mark Roethof
Tera Patron
Tera Patron

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

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Mark,

 

THis is what happ

find_real_file.png

 

Regards

Vaishali

 

msm4
Mega Guru

Hello VAishali,

You can actually write OnChange Client script to populate the values

for example :


g_form.setValue('department', g_user.getDepartmentID());




Surendra Poosa1
Kilo Expert

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);
}

The SN Nerd
Giga Sage
Giga Sage

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