Dynamically create URL link to a list of records using sys id

Bianca1
Giga Contributor

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?

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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.

find_real_file.png

When you choose Sys ID the qualifier to use is "one of"

find_real_file.png

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.

 

View solution in original post

6 REPLIES 6

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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.

find_real_file.png

When you choose Sys ID the qualifier to use is "one of"

find_real_file.png

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.

 

This is a great solution, however the Javascript join() function is not working for some reason.

thank you, this worked!

How to get the URL if the sys_ids are fetched dynamically and considering if there is a list of 200+ sys_id, the url would be long, is there a way to get the tiny url through script?