onchange caller autopapulate caller manager email and caller magagermobile number

aig20242025
Kilo Contributor

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;

// If caller is cleared → clear manager fields
if (!newValue) {
g_form.clearValue('u_manage_email');
g_form.clearValue('u_manager_mobile_number');
return;
}

// Fetch Caller record
g_form.getReference('caller_id', function(caller) {
if (!caller || !caller.manager) {
g_form.clearValue('u_manage_email');
g_form.clearValue('u_manager_mobile_number');
return;
}

// Fetch Manager record using manager sys_id
g_form.getReference(caller.manager, function(manager) {
if (manager) {
// Set Manager Email
if (manager.email) {
g_form.setValue('u_manage_email', manager.email);
} else {
g_form.clearValue('u_manage_email');
}

// Set Manager Mobile (default sys_user field is "mobile_phone")
if (manager.mobile_phone) {
g_form.setValue('u_manager_mobile_number', manager.mobile_phone);
} else {
g_form.clearValue('u_manager_mobile_number');
}
} else {
g_form.clearValue('u_manage_email');
g_form.clearValue('u_manager_mobile_number');
}
});
});
}

2 REPLIES 2

CharanV98428402
Giga Contributor

Hi @aig20242025 ,

In the above code, you are using the getReference()method, which ServiceNow does not recommend. Instead, you can follow the approach below. 

Why is getReference() not recommended

  1. Performance Issues

    • getReference() makes a server call directly from the client script each time it’s executed.

    • If multiple fields/scripts use it, you end up with multiple synchronous calls to the server → slows down form load/performance.

  2. Scalability Problems

    • In large implementations, too many getReference() calls can cause unnecessary round-trips to the server for each record lookup.

  3. Best Practice Shift

    • ServiceNow recommends using GlideAjax because it:

      • Batches logic into a single call.

      • Is asynchronous → non-blocking.

Keeps business logic in Script Includes (server-side), making it reusable, testable, and cleaner.
Client callable Script include:

var getManagerDetails = Class.create();
getManagerDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManagerDetails: function() {
        var callerSysId = this.getParameter('sysparm_caller_id');
        var result = {};

        if (!callerSysId) {
            return JSON.stringify(result);
        }

        var callerGR = new GlideRecord('sys_user');
        if (callerGR.get(callerSysId)) {
            var mgrGR = new GlideRecord('sys_user');
            if (mgrGR.get(callerGR.manager)) {
                result.email = mgrGR.email.getValue() || '';
                result.mobile = mgrGR.mobile_phone.getValue() || '';
            }
        }

        return JSON.stringify(result);
    },
    type: 'getManagerDetails'
});
 
On change Client Script : 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    if (!newValue) {
        g_form.clearValue('u_manage_email');
        g_form.clearValue('u_manager_mobile_number');
        return;
    }

    var ga = new GlideAjax('getManagerDetails');
    ga.addParam('sysparm_name', 'getManagerDetails');
    ga.addParam('sysparm_caller_id', newValue);

    ga.getXMLAnswer(function(response) {
        var manager = JSON.parse(response);

        if (manager && (manager.email || manager.mobile)) {
            if (manager.email) {
                g_form.setValue('u_manage_email', manager.email);
            } else {
                g_form.clearValue('u_manage_email');
            }

            if (manager.mobile) {
                g_form.setValue('u_manager_mobile_number', manager.mobile);
            } else {
                g_form.clearValue('u_manager_mobile_number');
            }
        } else {
            g_form.clearValue('u_manage_email');
            g_form.clearValue('u_manager_mobile');
        }
    });
}
 
Note: While creating a Client Callable Script Include, make sure to assign the appropriate role to the Script Include.

Why do we need to give a role to Client Callable Script Includes?

  1. Security Concern

    • A Client Callable Script Include can be called directly from the browser (via GlideAjax).

    • Without role restrictions, any logged-in user can trigger it and fetch sensitive data (like user info, emails, phone numbers, etc.).

    • This becomes a data exposure risk if not controlled.

  2. Principle of Least Privilege

    • By assigning a role, only authorized users can execute the logic.

Example: If only ITIL users should fetch manager details, restrict the Script Include with the itil role.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Charan V.



Ankur Bawiskar
Tera Patron
Tera Patron

@aig20242025 

it's an easy requirement.

Ensure manager field is reference type on your form

what debugging is done from your side?

try this once if manager field is string

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    if (!newValue) {
        g_form.clearValue('u_manage_email');
        g_form.clearValue('u_manager_mobile_number');
        return;
    }

    // Get caller details
    g_form.getReference('caller_id', function(caller) {
        if (!caller || !caller.manager) {
            g_form.clearValue('u_manage_email');
            g_form.clearValue('u_manager_mobile_number');
            return;
        }
        // Get manager details
        g_form.getReference('manager', function(manager) {
            // Note: the first param must be field name 'manager' not sys_id
            if (manager) {
                g_form.setValue('u_manage_email', manager.email || '');
                g_form.setValue('u_manager_mobile_number', manager.mobile_phone || '');
            } else {
                g_form.clearValue('u_manage_email');
                g_form.clearValue('u_manager_mobile_number');
            }
        }, caller);
        // Passing caller record as second param to scope getReference on it’s 'manager' field
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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