Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Auto-populate fields from sys_user in a custom application

gmacias
Tera Contributor

I am building a custom Onboarding Access Request application in ServiceNow.

I have a Reference field that points to the sys_user table. When a user selects a person's name, I want several fields to automatically populate with information from the selected user record, such as:

  • Email
  • Department
  • Training completion date

What is the recommended approach to accomplish this?

  • Should I use an onChange Client Script with g_form.getReference()?
  • Should I use a UI Policy?
  • Should I fix this when I move it to Service Catalog?
  • Is there a way to accomplish this using a Dependent field, or are Dependent fields only intended for filtering available choices?

I'm looking for the best practice that follows ServiceNow development standards and performs well.

6 REPLIES 6

Aditya_hublikar
Giga Sage

Hello @gmacias ,

 

There are 2 ways to achieve this 

1) onchange client script + glide ajax 

2) as you suggested onchange client script + getReference method 

 

 

onchange client script + glide ajax :  this follows ServiceNow best practices because it retrieves only the required fields, offers better performance, and provides more control over security and business logic.

 

 onchange client script + getReference method : it retrieves the entire record, so GlideAjax is generally preferred for better scalability.

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya

 

Hi @gmacias , This can be achieved using variables 'auto populate' related tab itself regardless of it is in custom application or in native UI. 
You will just have to use- javascript:gs.getUserID(); into you variable's 'auto populate' related list tab. This way you can autopopulate logged in user into the variable.
javascript:gs.getUserID();

 

If this response helps you mark it as accepted solution and give it a thumbs up 👍. This help me and community to find answers quickly.

Best Regards,

Saurabh V.

 

Nishant8
Tera Sage

Hello @gmacias , Use Client Script and Script Include combination to achieve this and don't use other approaches. And to answer the last point, yes you are right that Dependent field is used to create dependency and limit the records based on Dependent.

You can define the catalog item along with the desired variables in it and use below shared scripts. Although, I have commented the use of script, please ensure that you change the variable and user fields names accordingly.

- Script Include:

var getUserDetailsJSON = Class.create();
getUserDetailsJSON.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        // Retrieve the user sys_id passed from the client script
        var userSysId = this.getParameter('sysparm_user_id');
        var userObj = {};

        if (userSysId) {
            var grUser = new GlideRecord('sys_user');
            if (grUser.get(userSysId)) {
                userObj.first_name = grUser.getValue('first_name');
                userObj.last_name = grUser.getValue('last_name');
                userObj.email = grUser.getValue('email');
                userObj.phone = grUser.getValue('phone');
                userObj.department = grUser.getDisplayValue('department');
            }
        }
        return JSON.stringify(userObj);
    },

    type: 'getUserDetailsJSON'
});

 

- Client  Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Initialize GlideAjax calling our Script Include class
    var ga = new GlideAjax('getUserDetailsJSON');
    // Specify the method to run
    ga.addParam('sysparm_name', 'getUserDetails');
    // Pass the selected user's sys_id to the server
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(parseUserData);
}
// Callback function to process server response
function parseUserData(answer) {
    if (answer) {
        try {
            var userDetails = JSON.parse(answer);
            alert('User Name: ' + userDetails.first_name + ' ' + userDetails.last_name);
            if (userDetails.email) {
                g_form.setValue('email', userDetails.email);
            }
            if (userDetails.phone) {
                g_form.setValue('phone', userDetails.phone);
            }

        } catch (e) {
            jslog('Error parsing user data JSON: ' + e);
        }
    }
}

 

Hello @gmacias , If you have a simple requirement to map user fields to variables, please consider the approach shared by @Danish Bhairag2 , as it is better than making another round trip to the server using GlideAjax. However, if you want to apply some logic or transform some fields, consider the shared Client Script and Script Include combo

 

Regards,

Nishant