- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2020 11:08 AM
I have an array of sys ids and I want to dynamically create a URL to view a list of these records by appending the sys ids using OR statements.
Here is what I have so far:
// sysIds will contain array of sys ids
var server_URI = gs.getProperty('glide.servlet.uri');
var url = server_URI + '/nav_to.do?uri=%2Fhr_list.do%3Fsysparm_query%3Dsys_id%';
for( i = 0; i < sysIds.length; i++) {
url += i + 'OR';
}
However, at the end of the last sys id I want to add:
'%26sysparm_first_row%3D1%26sysparm_view%3D'
How can I achieve this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2020 11:28 AM
The easiest way to get the right query is in the UI itself. When viewing a list of records, use the filter and then copy the query.
When you choose Sys ID the qualifier to use is "one of"
Then when you copy the query you will get:
sys_idINb7707f7ddbb1c410d0a69875db96199e,54af6979db5fc410f73e50e4e2961977
As you can see it is using "IN" and then the ID's are separated by a comma. You can also right click on the breadcrumb and choose Copy URL:
https://INSTANCENAME.service-now.com/TABLENAME.do?sysparm_query=sys_idINb7707f7ddbb1c410d0a69875db96199e%2C54af6979db5fc410f73e50e4e2961977&sysparm_view=
Is is important to note that the order of the parameters does not matter so you could put '%26sysparm_first_row%3D1%26sysparm_view%3D' first
So back to your script given the list of sys_ids are separated by commas, with an array you can simply use the JavaScript join(",") function on the array and append it to your text string.
Please mark this post or any as helpful or the correct answer to your question if applicable so others viewing may benefit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2020 12:44 PM
Can we try?
var server_URI = gs.getProperty('glide.servlet.uri');
var url = server_URI + '/nav_to.do?uri=%2Fhr_list.do%3Fsysparm_query%3Dsys_id%3D';
for( i = 0; i < sysIds.length; i++) {
url += i + 'OR';
}
url+='%26sysparm_first_row%3D1%26sysparm_view%3D';
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2021 02:33 PM
If you want to add, '&sysparm_first_row=1&sysparm_view=', just paste it in the script below within the else statement:
else { this.queryPath = 'sys_id='+arr[i]+'&sysparm_first_row=1&sysparm_view='; }
var arr = [];
var gr = new GlideRecord('sys_user');
gr.addNotNullQuery('name');
gr.query();
while (gr.next()){
arr.push(gr.getDisplayValue('sys_id'));
}
// gs.print(arr);
var server_URI = gs.getProperty('glide.servlet.uri');
var url = server_URI + 'nav_to.do?uri=/sys_user_list.do?sysparm_query=';
var limit = 5;
for( i = 0; i < limit; i++) {
if (i < (limit-1)) { this.queryPath = 'sys_id='+arr[i]+'^OR'; }
else { this.queryPath = 'sys_id='+arr[i]; }
url += this.queryPath;
}
output:
https://[instance].service-now.com/nav_to.do?uri=/sys_user_list.do?sysparm_query=sys_id=00189b324f1e0200c0adfe618110c76d^ORsys_id=005d500b536073005e0addeeff7b12f4^ORsys_id=02826bf03710200044e0bfc8bcbe5d3f^ORsys_id=02826bf03710200044e0bfc8bcbe5d55^ORsys_id=02826bf03710200044e0bfc8bcbe5d5e
Limiting the scope of the query will help to avoid overloading the header with a monster string and having the scripted url returning this error:
With the scope of the query limited, the url that is built by the script successfully brings the end user to a valid list view: