How to return display value instead of sys_id

nicolemccray
Tera Expert

All other fields are returning as expected, instead of 'manager'.  How can I modify this to not return a sys_id?

function onChange(control, oldValue, newValue, isLoading)
{


var requester = '';
if(g_form.getValue('ConfirmInfoReqOtherChoice') == 'No') // Which means user requesting request for himself
{
requester = g_user.userID; // This gives login user id
}
else
{
requester = g_form.getValue('ConfirmInfoReqName');
}

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',requester);
user.query();
if ( user.next() )
{

g_form.setValue('manager',user.manager);
g_form.setValue('citizenship_type',user.u_employee_tag);
g_form.setValue('employee_number',user.employee_number);
g_form.setValue('level_3_number',user.u_business_unit_id);
g_form.setValue('level_5_number',user.u_division_id);
}

}

 

5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Hii,

 

Use 

 

g_form.setValue('manager',user.manager.getDisplayValue());

 

Ps: It is not advised that you use glide record in client side code

Check this link and see if you vcan use g_user object and achieve what you are trying to

https://www.servicenowguru.com/scripting/user-object-cheat-sheet/

-Anurag

 

-Anurag

You can't actually use getDisplayValue() in the client so this isn't an option.

It's highly recommended you use GlideAjax to get these values as a client side glide record will return the whole record rather than just the values you require, making it very inefficient. Added to that, when you set the value of a reference field in the client, if you only provide the sys_id, it has to make another lookup to the server to retrieve the display value.

There are plenty of GlideAjax guides out there but the one below is my favourite. It's a really useful method to be comfortable with so i would highly recommend getting to grips with it 🙂

https://community.servicenow.com/community?id=community_blog&sys_id=f8ccee25dbd0dbc01dcaf3231f961978

g_form.setValue('manager',user.manager.getDisplayValue()); did not work

Mark Roethof
Tera Patron
Tera Patron

Hi there,

For client scripting, you actually need to return 2 values: value (sys_id) + displayValue. Technically only one value works... though an additional call will be performed = bad performance.

So use something like:

g_form.setValue('manager', user.manager, user.manager.name);

Note: consider changing your script, you are using GlideRecord query which is not best practice in Client Scripting. Use g_user object instead, or GlideAjax (with getXMLAnswer) and Script Include instead.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn