Help with addJoinQuery()

ServiceNow SA
Kilo Guru

Hi Team,

I need to access object of the join table. Any help?

For eg: if we see below example, how can i access fields of incident table?

Also orderBy is not supposed to be working. When i am trying to print the order, it is coming as undefined:

 

// Look for Problem records that have associated Incident records
var gr = new GlideRecord('problem');
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');
grSQ.orderBy('order'); 
// Query
gr.query();

// Iterate and output results
while (gr.next()) {
gs.info("Order is: " + grSQ.order);
gs.info(gr.getValue('number'));
}

 

7 REPLIES 7

asifnoor
Kilo Patron

Hi,

The addJoinQuery allows you to fetch the records based on the join (it is like subquery). You can access the fields only from the table on which the GlideRecord is initialized.

Mark the comment as a correct answer and helpful if you find this helpful.

Ref: https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_ScopedGlideRecordAddJoinQuery_String...

 

So if i need to orderBy in the subquery it will not work?

No, it wont. The orderBy is used to display the results in a specific order. Since addjoinQuery is not the end result, orderBy does not work there. So in your example, you have to use OrderBy on the gr object.

Mark the comment as a correct answer and helpful if you find this helpful.

SatheeshKumar
Kilo Sage

Your script is not correct, share what exactly you are looking so that your script can be fixed!