Variables available in the "Query from" script on the "sys_relationship" table?

bbarrowes
Tera Contributor

Hi, what variables are available to the "Query from" script on the sys_relationship table?

I know that you use "answer" to return the name of the table that records should be queried from, but the script doesn't seem to have access to "current" or "parent", like you do in the "Query with" field.  If I try to get any information from the "current" or "parent" variables, I get errors related to that "current" and/or "parent" are null.

 

It seems like this "Query from" field is designed to allow you to dynamically select which table your records should be queried from.  How do you make it dynamic, if not by using the "current" or "parent" variables?

 

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Your question doesn't entirely make sense.  Creating a relationship creates a Related List that can be added to a form.  The table/form you intend to add it to is the "Applies to table", and the table that holds records that are related to this table is the "Query from table".  Both must be specified for the relationship to work.  The "Query with" script then defines/filters which records from the Query from table to show.  You can choose to show records not related to the current record, but typically the script is something like 

current.addQuery('opened_by', parent.sys_id);

where 'current' refers to the Query from table, so you are refining the query, and 'parent' is the dynamic record that is being displayed.  So you can add this relationship to a table like incident, and other task (extended) tables, but it wouldn't necessarily work to add it to a custom table, or something that didn't have the field in the addQuery.  Review the documentation for further clarification:

https://docs.servicenow.com/bundle/vancouver-platform-user-interface/page/administer/form-administra... 

(multiple posts because of new moderation flow, and inability to delete a post)

Brad, thanks for the response.  When you click the "Advanced" checkbox to true, the functionalities of the "Applies to table" and "Queries from table" are replaced by two script fields named "Apply to" and "Queries from".  What you have described for the "Query with" field would work perfectly with the "Advanced" checkbox set to false, but I am wondering what context variables are available to the "Apply to" and "Query from" script-type fields--context variables such as "current" and "parent", just like in the "Query with" field.  Attempting to access "current" and "previous" in those fields returns errors, so I'm wondering what we do have access to using, since it seems the only thing you can do is to hard code table names as the value for the "answer" context variable, without access to any other variables.

It would be great if there were actual system documentation on this.  Short of that, looking through the examples that use either or both of these fields on my PDI with various plugins installed, table_name is the only dynamic variable I can see, referring to the table name of the record being displayed.

 

You can use a GlideRecord or call a Script Include that returns a table name, to determine if the Related List should be shown on a particular record like this example:

BradBowman_0-1703247441831.png

Apply to seems to return true or false in all of the examples, and works with the Applies to table field shown above it, while Query with is hard-coded in all of the examples, which makes sense - if you're querying from a different table, that would change the Query with script and be a different relationship.