Display username using sysid

Sachin G K1
Kilo Sage

Hi All,

Below code is giving alert as undefined. Please help

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

//Type appropriate comment here, and begin script below
var requested_user_sysid = g_form.getValue('requested_for');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', requested_user_sysid);
gr.query();
gr.next();
alert(gr.name);

1 ACCEPTED SOLUTION

Musab Rasheed
Tera Sage
Tera Sage

Hi Sachin,

GlideRecord won't work in service portal , Kindly use Glidereference or GlideAjax, you will find lot of examples, Also there is one mistake in code you need to use if(gr.next()) instead of direct gr.next(), Mark my answer as correct if that helps.

var requested_user_sysid = g_form.getValue('requested_for');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', requested_user_sysid);
gr.query();
if(gr.next());
{
alert()
}

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

2 REPLIES 2

Musab Rasheed
Tera Sage
Tera Sage

Hi Sachin,

GlideRecord won't work in service portal , Kindly use Glidereference or GlideAjax, you will find lot of examples, Also there is one mistake in code you need to use if(gr.next()) instead of direct gr.next(), Mark my answer as correct if that helps.

var requested_user_sysid = g_form.getValue('requested_for');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', requested_user_sysid);
gr.query();
if(gr.next());
{
alert()
}

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Voona Rohila
Kilo Patron
Kilo Patron

Hi Sachin

It's not good practivce to use GlideRecord in client side scripting.

You can use getRefernce or GlideAjax 

examples:

https://docs.servicenow.com/en-US/bundle/sandiego-application-development/page/script/ajax/topic/p_A...


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP