query about addQuery() in widget scripting

Goofing_Off
Tera Contributor

Hey all!

 

I'm trying to see if I can use records in the value part of GlideRecord's addQuery() function. I already know I can do something like addQuery("column_name", integer) or addQuery("column_name", "string"), but there are some columns where the values are records from other tables. How do I go about putting a record in the value spot such that it looks something like addQuery("column_name", record)? Is this even possible with addQuery(), or at all?

 

Any help is greatly appreciated! 

1 ACCEPTED SOLUTION

Voona Rohila
Tera Patron

Hi @Goofing_Off 

 

You have to use the sys_id of the record if the values are records from other table( i.e reference field)

 

addQuery("column_name", "sys_id_of_record");

 

What is the use-case you are trying to acheive?

View solution in original post

4 REPLIES 4

Voona Rohila
Tera Patron

Hi @Goofing_Off 

 

You have to use the sys_id of the record if the values are records from other table( i.e reference field)

 

addQuery("column_name", "sys_id_of_record");

 

What is the use-case you are trying to acheive?

Goofing_Off
Tera Contributor

hi Rohila! I'm trying to filter a list of services from the cmdb_ci_service table using the "managed by" column. so far, the code looks like this:

var svs = new GlideRecord("cmdb_ci_service");
svs.addQuery("sys_class_name", "cmdb_ci_service");
svs.addQuery("operational_status", 1); 
svs.addQuery("used_for", "Production"); 
 
i'd like to add this:
 
svs.addQuery("managed_by", <record>);
 
when I added svs. addQuery("managed_by", "1234567890"); i didn't get any results. here, 1234567890 is the placeholder for the sys_id I get when I go into the table and right click -> select 'copy sys_id' on the record I want.

wait, I realized I was copying the wrong item. I should be going to the table the record originates from, entering the edit menu of the record itself, and right clicking the top grey bar to copy the proper sys_id. Thank you so much!

Yesss but It's not recommended to use sys_id directly, make it dynamic.

 

If you want to filter with 1 particular managed by then store that record sys_id in a property and use gs.getProperty('propertyname') to get the value.I future if you want to change managed by with some other record then you can simply update the property value.

svs.addQuery("managed_by", gs.getProperty('property_name')); // create a property with relevant name and use it here.