Problem to convert sys_id of reference field into string

Matki
Tera Contributor

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:

 

Matki_0-1667553498522.png

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!

 

 

6 REPLIES 6

RaghavSh
Kilo Patron
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

Matki
Tera Contributor

Hi,

thanks for your help.

Unfortunately it doesn't work

Matki_0-1667557278160.png

 

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

Brad Bowman
Kilo Patron
Kilo Patron

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:

 

https://www.servicenow.com/community/developer-articles/catalog-data-lookup-definition-on-any-table-... 

 

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.