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.

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

That's strange, I have tried what I believe to be exactly the same code but on the sn_agent_cmdb_ci_agent ttable and get a blank result:

result.JPG

Rich

Hi @Ryax1 ,

 

Please check the actual table name of the location table being referenced in the location field on the sn_agent_cmdb_ci_agent  table.

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

Yes I've just double checked and confirm it is definitely the cmn_location table being referenced.

Ankur Bawiskar
Tera Patron
Tera Patron

@Ryax1 

can you try this?

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.location.getDisplayValue();
gs.print(loc);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hey @Ankur Bawiskar ,

No luck with that code either I'm afraid, still returns blank like before 😞

Richard