Encoded query equivalent of addJoinQuery?

tmgarcia
Giga Contributor

I apologize if this has been asked and answered before, but in my searching I couldn't manage to find anything regarding this. I know this might be a stretch, but is there an Encoded Query syntax equivalent to what is accomplished through GlideRecord's addJoinQuery function? I've tried out simply building a GlideRecord that used addJoinQuery and then calling its getEncodedQuery, but it never quite returns something usable.

For example, if you wanted to accomplish the sample query used in the Wiki section on addJoinQuery:

// Look for Problem records

var gr = new GlideRecord('problem');

// That have associated Incident records

var grSQ = gr.addJoinQuery('incident');

// Where the Problem records are "active=false"

gr.addQuery('active', 'false');

// And the Incident records are "active=true"

grSQ.addCondition('active', 'true');

// Query

gr.query();

but rather than using GlideRecord, you were restricted to just supplying an Encoded Query, how would that work, or is it even possible?

1 ACCEPTED SOLUTION

tmgarcia
Giga Contributor

Thank you guys for the responses. It does seem that it's not possible to directly replicate with the encoded query syntax alone.



The one alternative I've found to accomplish similar functionality for specific cases is to make a javascript call out to a Script Include function that returns a collection of sys_ids (or whatever other field) from the table/query you'd join using GlideRecord's addJoinQuery. For example, a "My Requests" view of the Tasks table that makes a call to the SMFilters Script Include that comes with Service Management Core. The first query it uses is [Sys ID] [is one of] [javascript: new SMFilters().getRequestList(gs.getUserID());] or (in the actual query syntax) sys_idINjavascript: new SMFilters().getRequestList(gs.getUserID());. This would give you similar results to an addJoinQuery call where you joined a Task table query with Request table, going by the sys_id and adding conditionals for the opened_by and requested_for fields.



This seems mostly optimal for very specific use cases like this, but I could definitely see, with a bit of work, being able to make a very generalized reusable script include that could take in the arguments and return the same functionality as addJoinQuery. Not sure my needs are quite enough to go down that route just yet, but if I do, I'll come back here and get down what I wrote for any future developers who happen to come by this question.


View solution in original post

4 REPLIES 4

ramireddy
Mega Guru

I don't think, this is possible to convert addJoinQuery example into EncodedQuery. Even if we look at ServiceNow UI, encoded queries are used to filter with in a table, But not to filter records through a condition based on another table.


Gurpreet07
Mega Sage

Hi Taylor,



Why to use join if you could get data using dot walking.



var grInc = new GlideRecord('incident');


var eq = grInc.addQuery('problem.active',false);


eq.addOrCondition('active',true);


grInc.query();



// Encoded query (Applicable on Incident)


gs.log("Encoded query : "+grInc.getEncodedQuery())



//getting data of inc and Problem



while(grInc.next()){


                  incNum = grInc.number ;


                  prbNum = grInc.number ;


}


tmgarcia
Giga Contributor

Thank you guys for the responses. It does seem that it's not possible to directly replicate with the encoded query syntax alone.



The one alternative I've found to accomplish similar functionality for specific cases is to make a javascript call out to a Script Include function that returns a collection of sys_ids (or whatever other field) from the table/query you'd join using GlideRecord's addJoinQuery. For example, a "My Requests" view of the Tasks table that makes a call to the SMFilters Script Include that comes with Service Management Core. The first query it uses is [Sys ID] [is one of] [javascript: new SMFilters().getRequestList(gs.getUserID());] or (in the actual query syntax) sys_idINjavascript: new SMFilters().getRequestList(gs.getUserID());. This would give you similar results to an addJoinQuery call where you joined a Task table query with Request table, going by the sys_id and adding conditionals for the opened_by and requested_for fields.



This seems mostly optimal for very specific use cases like this, but I could definitely see, with a bit of work, being able to make a very generalized reusable script include that could take in the arguments and return the same functionality as addJoinQuery. Not sure my needs are quite enough to go down that route just yet, but if I do, I'll come back here and get down what I wrote for any future developers who happen to come by this question.


Very late reply I know, but in case anyone else is looking at this sort of issue this might be helpful...

 

https://www.servicenow.com/community/developer-blog/gql-glide-query-language-part-8-helsinki-studio-...