Using Arrays in Glide Records

stevethomas
Giga Contributor

I want to share this Glide Record with the community.  I discovered this while experimenting with a Glide Record.  I did not want to write multiple addorConditions so I came up with this.  I have been testing arrays in Glide Records for about a month and it has always worked.  My question is, how does the Glide Record loop through the array?  Please feel free to try this yourself.  It also works with !=, CONTAINS, DOES NOT CONTAIN .

 

var count = 0;
var REQs = ['5', '2', '3'];
var assnmGroup = ['Service Desk', 'Business Systems']
var gr = new GlideRecord('sc_request');
gr.addQuery('request_state', REQs);
gr.addQuery('assignment_group.name', assnmGroup);
gr.setLimit(130);
gr.query();

while (gr.next())
{
gs.log("number: " + gr.number);
gs.log("Request State: " + gr.request_state);
gs.log("Assignment Group: " + gr.assignment_group.name);
gs.log("Short Description: " + gr.short_description);
gs.log("*****************************");
count++;
}
gs.log("Count: " + count);

 

Snipped of the output.

*** Script: number: REQ0048208
*** Script: Request State: 3
*** Script: Assignment Group: Business Systems
*** Script: Short Description: UltiPro Time Base update
*** Script: *****************************
*** Script: number: REQ0048551
*** Script: Request State: 5
*** Script: Assignment Group: Business Systems
*** Script: Short Description: ODS Growth Curve: Add Modifiers
*** Script: *****************************
*** Script: number: REQ0048511
*** Script: Request State: 5
*** Script: Assignment Group: Business Systems
*** Script: Short Description: HS SharePoint Site
*** Script: *****************************
*** Script: number: REQ0048632
*** Script: Request State: 3
*** Script: Assignment Group: Business Systems
*** Script: Short Description: AcuTrak: DNA Sample Inv - Bulk Enter Samples - Add Current State to Results Grid
*** Script: *****************************
*** Script: number: REQ0048665
*** Script: Request State: 2
*** Script: Assignment Group: Service Desk
*** Script: Short Description: Global Protect VPN
*** Script: *****************************
*** Script: number: REQ0048660
*** Script: Request State: 5
*** Script: Assignment Group: Service Desk
*** Script: Short Description: Print from PigChamp
*** Script: *****************************
*** Script: number: REQ0028810
*** Script: Request State: 5
*** Script: Assignment Group: Service Desk
*** Script: Short Description: On-boarding Request
*** Script: *****************************
*** Script: number: REQ0048605
*** Script: Request State: 3
*** Script: Assignment Group: Service Desk
*** Script: Short Description: Quantity threshold breached: UPS Battery
*** Script: *****************************
1 ACCEPTED SOLUTION

Cool, thanks for posting, i didn't know that addQuery would default to IN if you gave it an array. Please mark an answer correct to close the thread down.

View solution in original post

11 REPLIES 11

Dylan Mann1
Giga Guru

Very interesting! I didn't think this would work. I assume it handles it as a "IN" query, like:

gr.addQuery("request_state", "IN", [5,2,3]);

Thanks for sharing

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I assume it internally uses an IN operator to handle this for an array.

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

stevethomas
Giga Contributor

See what you are saying about the "IN".  But it works with gr.addQuery('request_state', '!=', REQs); So is the "!=" operator, used with the "IN" operator?  

Dubz
Mega Sage

If you add in a line like:

gs.log("query: " + gr.getEncodedQuery());

The we'll be able to see exactly what query is being submitted by that condition.