Combine gliderecord queries

codedude
Mega Expert

I have 4 queries... 2 going to incident table, 1 going to sc_request and the last going to sc_task. Is there a way that I can combine these 4 into 1? The reason being is I am wanting to order the results based on a datetime field for all 4 queries. I have 4 while loops looping through the results, it only orders each while loop, not the whole result set from all 4 queries...

Basically what I am trying to do is order the results of all 4 queries together, not individually... right now it is ordering them individually, so when I display it in a table it is not showing an accurate order.

FYI: This is in a UI Page.... so maybe I am a little handcuffed on what I can do...

If combining the queries is not the best way, can someone explain to me a way that I can achieve this in Servicenow?

<g:evaluate>

              var inc = new GlideRecord('incident');

              inc.addQuery('is_valid', 'true');

              inc.addQuery('active', 'true');

              inc.orderBy('logged_time');

              inc.query();

              var incUpdate = new GlideRecord('incident');

              inc2.addQuery('active', 'true');

              inc2.addQuery('type', 'First Location');

              inc2.addQuery('state', 'Open');

              inc2.orderBy('date');

              inc2.query();

              var req= new GlideRecord('sc_request');

              req.addQuery('active', 'true');

              req.addQuery('type', 'First Location');

              req.addQuery('state', 'Open');

              req.orderBy('date');

              req.query();

              var tsk = new GlideRecord('sc_task');

              tsk .addQuery('active', 'true');

              tsk .addQuery('type', 'First Location');

              tsk .addQuery('state', 'Open');

              tsk .orderBy('date');

              tsk .query();

</g:evaluate>

1 ACCEPTED SOLUTION

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Josh,



Is there any relation between the data of these tables you are querying? If there is then you could maybe do it with a Database view?



Also, I see the last 3 GlideRecords calls are with same filters, and taking into account that all these tables are extensions of task, can't you do only one GlideRecord call on task with a sys_class_name condition to be table incident/sc_request/sc_task?



Regards,


Sergiu


View solution in original post

7 REPLIES 7

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Josh,



Is there any relation between the data of these tables you are querying? If there is then you could maybe do it with a Database view?



Also, I see the last 3 GlideRecords calls are with same filters, and taking into account that all these tables are extensions of task, can't you do only one GlideRecord call on task with a sys_class_name condition to be table incident/sc_request/sc_task?



Regards,


Sergiu


Well the 3 tables have the state and type field in common...


May be instead of data being displayed  directly,  push the data in a common array, sort it and display it.

I would do that if I were developing this in anything other than Servicenow... unsure how to do this in Jelly scripting... can you provide an example?



This is how I am performing the while loop:



<j:while test="${inc.next()}">


  <tr>


  <td style="text-align: left; font-size: 20px;">${inc.caller_id.name}</td>


  <td style="text-align: left; font-size: 20px;">${inc.number}</td>


  <td><a class="linked" target="_blank" style="text-decoration: none; color: #666666 !important;" href="nav_to.do?uri=incident.do?sys_id=${inc.sys_id}"><button class="btn btn-success" style="width:140px; font-size: 16px;"><span class="glyphicon glyphicon-search" style="padding-right: 6px;"></span>View incident</button></a></td>


  </tr>


</j:while>