Problem to convert sys_id of reference field into string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 02:54 AM
Hello everyone,
my goal is to display the cost center of the current requested for automatically in the Service Portal.
Therefore, I map the field cost_centers_clock (reference field) with the cost_center field from the user record in a client script:
How can I convert the sys_id into the name?
My code:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', newValue);
gr.query(myCallbackFunction);
function myCallbackFunction(gr) {
if (gr.next()) {
g_form.setValue('cost_centers_clock', gr.cost_center);
}
}
}
I have found several solutions, but they have not worked for me.
g_form.setValue('cost_centers_clock', gr.getDispayValue('cost_center'));
g_form.setValue('cost_centers_clock', gr.getDispayValue('cost_center.name'));
gr.cost_centers_clock.getDisplayValue()
// Use of GlideAjax...
Can anyone help me?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:07 AM
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', newValue);
gr.query(myCallbackFunction);
function myCallbackFunction(gr) {
if (gr.next()) {
g_form.setValue('cost_centers_clock', gr.cost_center.name);
}
}
}
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:21 AM
Hi,
thanks for your help.
Unfortunately it doesn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 03:28 AM - edited 11-04-2022 03:29 AM
You should switch to GlideAjax or use below:
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', newValue);
gr.query(myCallbackFunction);
function myCallbackFunction(gr) {
if (gr.next()) {
var gr1 = new GlideRecord('cmn_cost_cente')
gr1.addQuery('sys_id',gr.cost_center);
gr1.query(myCallbackFunction1);
function myCallbackFunction1(gr1) {
g_form.setValue('cost_centers_clock', gr1.name);
}
}
}
}
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 04:12 AM
Consider changing the variable to a reference type. This will maintain the integrity of the Cost Center value for whatever it is used for in this request, and if the requestor or fulfiller is permitted to change this value, they can do so using the reference list instead of free-form typing. As an alternative to the unsupported script, take a look at this excellent article on how to lookup related data without a script:
I haven't tried this, but if you're going to leave Cost Center as a text field, you could define a lookup on the User's Cost Center, populating that in a reference variable that is hidden on the form, then have another lookup defined to populate Cost Center (name) from the Cost Center reference that was just populated.