Auto Populate User Details through Different Methods


Populate User Details through Different Methods

First method

The fastest way to populate user details is by using JavaScript wizardry like this:

  • javascript:gs.getUserID(); // user sys_id 
  • javascript:gs.getUserName(); // user name
  • javascript:gs.getUser().getEmail(); // user email address
We can call them JavaScript filters. These filters will populate the logged-in user's sys_id, user name, and email address.

To make it work you need to insert these into the default value field of the respective variables.

References:
  • https://www.servicenow.com/community/developer-forum/how-to-auto-populate-fields-for-logged-in-user/m-p/1962339/page/3
  • https://www.youtube.com/watch?v=8jvrvdx5F2Y&ab_channel=ServiceNerd

Second method

Another way to populate user details is by using Client Scripts.

Example:

function onLoad() {
    var currentUser = g_user.userID; // save logged-in user sys_id in currentUser
    if (g_form.isNewRecord()) // check if form is for new record{
        g_form.setValue('caller_id', currentUser);
    }
}

function onLoad() {
    var currentUser = g_user.userID; // save logged-in user sys_id in currentUser
    if (currentUser != '') // check if logged-in user is not empty{
        g_form.setValue('caller_id', currentUser);
    }
}

This method is for having more control over when to populate the user details.

You can also do it via UI Policies & UI Actions of course.

References:
  • https://www.youtube.com/watch?v=datpHpPj6bE&ab_channel=SAASWITHSERVICENOW
  • https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0745287
  • https://www.reddit.com/r/servicenow/comments/egewz2/autopopulate_email_field_from_name_field_in_new/
  • https://www.servicenow.com/community/itsm-forum/time-worked-autofill-user-by-logged-in-user/td-p/586606

Third method

If you really want to go Berzerk you can go for the AJAX API to populate user attributes.

Note: this method goes usually hand in hand with Catalog Client Scripts (see Second Method). 

References:
  • https://www.servicenow.com/community/developer-articles/auto-populate-user-details-using-ajax/ta-p/2315390
  • https://docs.servicenow.com/bundle/vancouver-platform-administration/page/administer/field-administration/concept/c_AutoCompleteForReferenceFields.html
  • https://www.youtube.com/watch?v=jAAClJ0Ld6o&ab_channel=TechnoMonk
  • https://www.youtube.com/watch?v=MgXuzjNAei8&ab_channel=LearnServiceNowwithRavi

Fourth method

Another method is to use the GlideUser API.

Reference:
  • https://www.servicenow.com/community/developer-blog/quot-fpcuser-quot-a-more-useful-user-object/ba-p/2265928

Fifth method

Since Utah release, there has been a new auto-populate functionality that makes things even easier to populate user details.

Note: this functionality only applies to catalog item variables. 

References:
  • https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-reference-type-variable-utah/ta-p/2475511
  • https://www.youtube.com/watch?v=qT8gHMObuJQ&ab_channel=NayeemServiceNowBlog
NOTE:

The above methods are all about auto-populating user details when a FORM is loading. 

There is also the other way around whereby auto-population of info is done by submitting an incident or request from the Service Portal.

Reference:
  • https://www.servicenow.com/community/developer-articles/auto-populate-quot-contact-type-quot-field-to-quot-self-service/tac-p/2766723

Comments

Popular Posts