Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Getting value of the field instead of sys_id

ravali6
Giga Contributor

I have a script where I do a GlideRecord query to the request table. Once I get the records, I am getting to the value of the opned by field. But since opened by is a reference field, it is returning sys_id. How can I get the displayed value ?

Below is the script :

var gr = new GlideRecord("sc_req_item");
gr.addQuery("u_requested_for", g_form.getValue("emp_name"));
gr.addQuery("stage",'!', "waiting_for_approval");
gr.query();

if(gr.next())
{
alert("A request has already been submitted by " + gr.opened_by); // this is returming sys_id, I need the value

}


Please let me know if anyone has any suggestions.

22 REPLIES 22

manojprabaakars
Kilo Contributor

For reference field type, it will display sys_id only. Using gr.getDisplayValue('reference_field_name') or gr.reference_field_name.getDisplayValue() will give you the expected value.

 

Here in your case use gr.getDisplayValue('opened_by');

 

Please hit helpful/mark as answer if this really does :).

Archana Reddy2
Tera Guru

Hi Ravali,

Try with the below code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var gr = new GlideRecord("sc_req_item");
	gr.addQuery("requested_for",g_form.getValue("emp_name"));
        gr.addQuery("stage",'!', "waiting_for_approval");
	gr.query();
	
	if(gr.next())
		{
		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id',gr.opened_by);
		user.query();
		if(user.next())
			{
			alert("A request has already been submitted by " + user.name);
		}
	}
	
}

Hope this helps.

Thanks,

Archana

Swarup Patra
Kilo Guru


Sure, you can get the display value of a reference field in ServiceNow using the getDisplayValue() method. Here's how you can do it:

1. First, you need to create a GlideRecord object for the table you want to query. In your case, it's the request table. Here's how you can do it:

javascript
var gr = new GlideRecord('sc_request');


2. Next, you need to add a query to get the specific record you want. For example, if you want to get a record with a specific sys_id, you can do it like this:

javascript
gr.addQuery('sys_id', 'your_sys_id_here');
gr.query();


3. Once you have the record, you can get the sys_id of the opened by field like this:

javascript
if (gr.next()) {
var openedBySysId = gr.opened_by;
}


4. Now, to get the display value of the opened by field, you can use the getDisplayValue() method like this:

javascript
if (gr.next()) {
var openedByDisplayValue = gr.opened_by.getDisplayValue();
}


5. Finally, you can print the display value to the console to check if it's correct:

javascript
gs.info(openedByDisplayValue);


So, the complete script would look like this:

javascript
var gr = new GlideRecord('sc_request');
gr.addQuery('sys_id', 'your_sys_id_here');
gr.query();
if (gr.next()) {
var openedByDisplayValue = gr.opened_by.getDisplayValue();
gs.info(openedByDisplayValue);
}


Remember to replace 'your_sys_id_here' with the actual sys_id of the record you want to query.


nowKB.com
If you want to know any information about Service Now . Visit to https://nowkb.com/home