addJoinQuery method in GlideRecord

psrpar
Kilo Explorer

can any one explain that method ?

what will you get result over the this code and explain this code?

var prob = new GlideRecord('problem');

prob.addJoinQuery('incident');

prob.query();

4 REPLIES 4

Frank1975
Kilo Guru

HI Pida,


in the Wiki the AddJoinQuery is pretty good explained.



https://wiki.servicenow.com/index.php?title=GlideRecord#addJoinQuery&gsc.tab=0



Adds a filter to return records based on a relationship in a related table.
For example, find all the users that are in the database group (users via sys_user_grmember table). Another example would be find all problems that have an assigned incident (problems via the incident.problem_id relationship).
Note: this is not a true database join; rather, addJoinQuery adds a subquery. So, while the result set is limited based on the join, the only fields that you have access to are those on the base table (those which are in the table with which the GlideRecord was initialized).
Example:
Find problems that have incidents associated where the incident caller_id field value matches that of the problem opened_by field.


var gr = new GlideRecord('problem'); gr.addJoinQuery('incident', 'opened_by', 'caller_id'); gr.query();

hope this helps


Frank


johnram
ServiceNow Employee
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: GlideRecord



Visit http://docs.servicenow.com for the latest product documentation


amaradiswamy
Kilo Sage

Hi Sreenu,



It's actually joins two tables, in the above script first you are querying problem table and then you are adding incident table, that means if there are any related records in incident table it will return.



For example if i want to return list of problems with active is true and their related incident active field as false, then i could use the below script to query both the tables at a time.



// 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', 'true');



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


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



// Query


gr.query();



// Iterate and print results


while (gr.next()) {


      gs.print(gr.getValue('number'));


}


bablu3
Kilo Contributor

Hi sreenu,



var prob = new GlideRecord('problem');


prob.addJoinQuery('incident');


prob.query();



Generally in an ITIL process as you know any unexpected failure of a service is considered as an incident and if that incident repeats this will be considered as a problem so for every problem there will be those   similar incidents attached which were responsible for creation of this problem ticket.


So to know such kind of problems with an associated incident we use this code



var prob = new GlideRecord('problem');//we are creating an instance of the GlideRecord class on the problem table so that whatever the methods have been defined on this class(Glide Record) can be used on the problem table for doing database operations(CRUD) Instead of writing SQL queries


prob.addJoinQuery('incident');//This add a filter to return the problem records with associated incidents


prob.query();//Runs the query



Hope you understood.