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

Get Relationship Related Lists via script and refineQuery function (query_with)

Liam Barksdale
Tera Contributor

I'm trying to retrieve the contents of Related Lists via script. Not any specific Related List, but any list. Many Related lists are created via sys_relationship, and while it's simple to get data from some, using the current element sys_id, the table ( query_basic_from) and the reference_field. 

However, reference_field is not always populated, and the query is further refined via script, which works fine if you are on the form of the item you are looking at, but if you are trying to get that data via script, I can't figure out how to use the query_with field in a script to get the values.

Compounding the issue, I think, is that some refined queries might call a script include, sometimes in another scope, and I have noticed that some of the OOB scripts don't always specify the scope where the script include is (I just happened to notice that the other day when looking at two different GRC relationships - one uses the scope prefix but another doesn't)

 

The idea I have is to get the relationship record for the table of the element you are looking at from sys_relationship, check to see if there is a value in the query_with field, then do a GlideRecord against the basic_query_from field with your element as the sys_id to check, and the refined query in an addQuery or addEncodedQuery statement. I'm just not sure how to utilise the query_with field for this purpose.

 

Has anyone done this? I feel like I've done a pretty exhaustive search thus far, but perhaps I missed something.

 

Thank you

12 REPLIES 12

Ok! Finally got this figured out. Needed to use evaluator.getVariable('current') 

 

var vars = {
"current" : currentGR,
"parent" : parentGR
};
//Evaluate the script from the field
var evaluator = new GlideScopedEvaluator();
evaluator.evaluateScript(att, 'query_with', vars);
var result = evaluator.getVariable('current');

gs.info("result: " + result.getEncodedQuery());

 

Thank you for all of your help! I really appreciate it. 

I'm a little surprised that you had to do it that way when objects are passed by reference in Javascript.

But glade you got it working.

 

Luis Franco1
Tera Contributor

Maybe this could be used to create the Export options in Relationships Related Lists? This has been a long wanted feature of every servicenow user.

The only solution was servicenow guru's option but didn't have a solution for relationships:
Enabling Related List Exports in ServiceNow - ServiceNow Guru