The CreatorCon Call for Content is officially open! Get started here.

Show Display Values from reference field in Glide Record query

Ryax1
Tera Guru

Hi all,

I am trying to run a query on the Agent Client Collectors table and then get the display value of the location field. I have tried with the below script however the result is blank:

 

    var svs = new GlideRecord("sn_agent_cmdb_ci_agent");
    svs.addQuery('agent_extended_info.cmdb_ci.sys_class_name', 'cmdb_ci_computer')
	svs.query();
	while (svs.next()) {

		var loc = svs.getDisplayValue("location");
		gs.print(loc);
	}

 

 If I change the loc variable to show the sys id it prints it perfectly:

 

var loc = svs.getDisplayValue("sys_id");​

 

From what I can see the issue is because the location field is a referenced field as other reference fields also do the same thing. 

One solution I thought of is getting the sys_id and then querying the cmn_location table using that sys_id but this seems inefficient.

Please could someone with a little more scripting knowledge than myself explain why this is happening and if their is a simpler solution than my one proposed above?

Many thanks

Rich

1 ACCEPTED SOLUTION

Ryax1
Tera Guru

Hi @Ankur Bawiskar and @Sai Shravan ,

Thanks for both of your help, I have found the cause of this issue which was the Location field I was referencing, it needed to be "agent_extended_info.cmdb_ci.location" instead of just 'location'.

 

Here is the working script:

var svs = new GlideRecord("sn_agent_cmdb_ci_agent");
    svs.addQuery('agent_extended_info.cmdb_ci.sys_class_name', 'cmdb_ci_computer')
	svs.query();
	while (svs.next()) {

		var loc = svs.getDisplayValue("agent_extended_info.cmdb_ci.location");
		gs.print(loc);
	}

Rich

View solution in original post

10 REPLIES 10

Sai Shravan
Mega Sage

Hi @Ryax1 ,

 

The issue is happening because the "location" field on the "sn_agent_cmdb_ci_agent" table is a reference field.

 

var svs = new GlideRecord("sn_agent_cmdb_ci_agent");
svs.addQuery('agent_extended_info.cmdb_ci.sys_class_name', 'cmdb_ci_computer');
svs.query();
while (svs.next()) {
    var loc = new GlideRecord('cmn_location');
    loc.get(svs.location.sys_id);
    gs.print(loc.getDisplayValue());
}

 

Regards,
Shravan
If my answer solved your issue, please mark my answer as Correct & Helpful

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Ryax1 ,

If I've helped guide you correctly in this thread, please also consider marking it as "Accept Solution".

Thanks and take care!

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan ,

Thanks for the suggestion, this was my thought too but I wasn't sure if there was a more efficient way to do it.

Unfortunately I've just tried the code you suggested and still doesn't return the display value of the location, does this work for you?

Thanks

Rich

HI @Ryax1 ,

 

I have tried on cmdb_ci table, below snippet is for the reference.

SaiShravan_0-1678460617085.png

SaiShravan_1-1678460638289.png

 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you