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.

Question onChange client script on the reference field gets changed

Amrita Dey
Tera Contributor

In client script onChange logic if we have changed the Caller name, as Caller field is a reference field in the onChange function the oldValue or the newValue is giving the sys_id of the field not the exact name of the caller, now the name of the Caller we can get from the User table, so how we will fetch this username from the client script, so the username should be displayed as an alert message when the caller field gets changed?

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Amrita Dey 

this is simple

use this to get the display value of caller_id user

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

    var displayName = g_form.getDisplayBox('caller_id').value;
    alert(displayName);
}

If you want to get the user_name then use this script

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

    var callerUserName = g_form.getReference('caller_id').user_name;
    alert(callerUserName);
}

💡 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

2nd one solution is working , but my query is "if I'm changing the value of caller field then in the following function 

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

    //Type appropriate comment here, and begin script below
    var username = g_form.getReference('caller_id').user_name;
    confirm(username);


}
 
oldValue is the sys_id of the old caller, newValue is the sys_id of the new caller, now from the getReference is taking the parameter of the field name like "caller_id", but the basis of the sys_id how I will get the caller full name.