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

Danish Bhairag2
Tera Sage

Hi @gmacias ,

 

The best way would be to use the Autopopulate feature in service catalogs where no scripting is required.

 

Thanks,

Danish Bhairagdar 

Tanushree Maiti
Tera Patron

Hi @gmacias 

 

Try any option as given below:

 

Option 1:

  1.Create a Script Include

  • Navigate to System Definition > Script Includes>Click New.
  • Set Client callable to True.

 

var UserDataAjax = Class.create();

UserDataAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserInfo: function() {

        var userId = this.getParameter('sysparm_user_id');

        var result = {};

        var userGR = new GlideRecord('sys_user');

        if (userGR.get(userId)) {

            result.email = userGR.getValue('email');

            result.department = userGR.getValue('department');

            result.training_date = userGR.getValue('u_training_completion_date'); // Replace with your actual field name

        }

        return JSON.stringify(result);

    },

    type: 'UserDataAjax'

});

 

2: Create the onChange Client Script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    var ga = new GlideAjax('UserDataAjax');

    ga.addParam('sysparm_name', 'getUserInfo');

    ga.addParam('sysparm_user_id', newValue);

    ga.getXMLAnswer(function(response) {

        var data = JSON.parse(response);       

        g_form.setValue('YOUR_EMAIL_FIELD_NAME', data.email);

        g_form.setValue('YOUR_DEPARTMENT_FIELD_NAME', data.department);

        g_form.setValue('YOUR_TRAINING_DATE_FIELD_NAME', data.training_date);

    });

}

 

Option 2:

  • Navigate to All > System Definition > Client Scripts>Click New to create a script. 
  • Name: Populate User Details
    • Table: [Select your custom application table]
    • UI Type: All (ensures it works on Desktop and Mobile/Service Portal)
    • Type: onChange
    • Field name: [Select your Reference field pointing to sys_user]

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    g_form.getReference('u_employee_name', populateUserDetails); // Replace 'u_employee_name' with your reference field backend name

}

function populateUserDetails(userRecord) {

    if (userRecord) {

        g_form.setValue('u_email', userRecord.email); // Replace 'u_email' with your email field backend name

        g_form.setValue('u_department', userRecord.department); // Replace 'u_department' with your department field backend name

                g_form.setValue('u_training_date', userRecord.u_training_completion_date); // Replace 'u_training_date' and 'u_training_completion_date' with your exact backend names

    }

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti