OnChange Client Script on Assigned to field returns sys_id of newValue

captsiddu
Tera Contributor

Hi Team, I want to alert when Assigned To field is changed. For that I have written onChanage Client Script on Assigned To field.

______________________________________________________________________

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

    alert(newValue);

}
----------------------------------------------------------------------------------------
But when  Assigned To field is changed, I'm getting Sys Id of user in Alert..
How can I alert User name of newValue..???

1 ACCEPTED SOLUTION

palanikumar
Mega Sage

Hi,

By default this will store only the sys_id of the reference field. If you like to print the display value, then use the below code :

alert(g_form.getDisplayBox('field_name'));

Note: Replace field_name with actual field name 

Thank you,
Palani

View solution in original post

3 REPLIES 3

SyamPrasanM
Tera Expert

Hello @captsiddu 

 

I hope this Answer is useful to your Question,

 

To alert the username instead of the Sys ID when the "Assigned To" field is changed in ServiceNow, you'll need to retrieve the username associated with the new Sys ID. You can do this by making a g_form query to get the user’s display name

 

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

    // Fetch the user record based on the Sys ID of 'assigned_to'
    g_form.getReference('assigned_to', function(user) {
        if (user) {
            // Alert the username with a custom message
            alert('The task is currently assigned to: ' + user.name);
        } else {
            alert('There is no user currently assigned to this task.');
        }
    });
}
 
Screenshot 2024-10-28 114437.png

 

Screenshot 2024-10-28 114523.png
Best Regards,
Mule Syam,
PRAVAL-LOGO.png

palanikumar
Mega Sage

Hi,

By default this will store only the sys_id of the reference field. If you like to print the display value, then use the below code :

alert(g_form.getDisplayBox('field_name'));

Note: Replace field_name with actual field name 

Thank you,
Palani

Hi PalaniKumar, I did little changes to your code and it worked fine.
alert(g_form.getDisplayBox(
'caller_id').value);
 
Thank You!!