Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help with populating user table reference field values in catalog item

Arka Banerjee1
Tera Contributor

Hi Team,

 

I have the below catalog client script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    // Prevent running on load or when cleared
    if (isLoading) return;

    // Clear if no user selected
    if (!newValue) {
        g_form.clearValue('user_name');
        g_form.clearValue('user_id');
        g_form.clearValue('first_name');
        g_form.clearValue('last_name');
        g_form.clearValue('internet_address');
        g_form.clearValue('employee_type');
        g_form.clearValue('title');
        g_form.clearValue('work_phone');
        g_form.clearValue('location');
        g_form.clearValue('Center_Code');
        g_form.clearValue('Department_Name');
        g_form.clearValue('Department_Code');
        g_form.clearValue('Reporting_Manager');
        g_form.clearValue('Reporting_Manager_Phone');
        return;
    }

    // Fetch the referenced user record; works in SP & UI16
    g_form.getReference('enter_x_id', function(user) {
        if (!user) {
            g_form.clearValue('user_name');
            g_form.clearValue('user_id');
            g_form.clearValue('first_name');
            g_form.clearValue('last_name');
            g_form.clearValue('internet_address');
            g_form.clearValue('employee_type');
            g_form.clearValue('title');
            g_form.clearValue('work_phone');
            g_form.clearValue('location');
            g_form.clearValue('Center_Code');
            g_form.clearValue('Department_Name');
            g_form.clearValue('Department_Code');
            g_form.clearValue('Reporting_Manager');
            g_form.clearValue('Reporting_Manager_Phone');
            return;
        }
        // Populate your text variables
        g_form.setValue('first_name', user.first_name || '');
        g_form.setValue('last_name', user.last_name || '');
        g_form.setValue('user_name', user.name || '');
        g_form.setValue('user_id', user.user_name || '');
        g_form.setValue('internet_address', user.email || '');
        g_form.setValue('employee_type', user.u_associate_role || '');
        g_form.setValue('title', user.title || '');
        g_form.setValue('work_phone', user.phone || '');
        g_form.setValue('location', user.location || '');
        g_form.setValue('Center_Code', user.cost_center || '');
        g_form.setValue('Department_Name', user.u_department_description || '');
        g_form.setValue('Department_Code', user.u_department_cd || '');
        g_form.setValue('Reporting_Manager', user.manager || '');
        g_form.setValue('Reporting_Manager_Phone', user.manager.phone || '');
    });
}
 
In this the location, center code and reporting manager are returning only the sys ID's(all reference fields) and the reporting manager phone is blank. Please help with how I can fix this.
 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Arka Banerjee1 

Issue in your script

-> you cannot dot walk 1 more level in getReference() hence manager.phone won't come

-> location, center code and reporting manager seems to be string type, make them reference type referring to correct table and it will work

OR -> Easy approach

you can use Auto populate feature to populate other variables based on reference variable and no scripting required

Ensure you create correct variable types

Example: if you are dot walking and bringing phone then you should have string variable

if you are dot waling and bringing some reference value then your variable should be reference type referring to correct table

Auto-populate a variable based on a reference type variable (Utah) 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Nawal Singh
Tera Guru

Hi @Arka Banerjee1 ,

dot-walking does NOT work client-side (ex: user.manager.phone)

This is why:--

  • location - sys_id
  • center_code (cost center) - sys_id
  • reporting_manager - sys_id
  • reporting_manager.phone - blank

Because the referenced fields do not auto-expand on the client.

 

Use GlideAjax to retrieve expanded values!