Shortening of encoded query

Amit160
ServiceNow Employee
ServiceNow Employee

We are creating URL based on an encoded query. In encoded query, there is 'IN' operator on which we are filtering long string of sys Ids. The number of sys Ids in some case exceeds more than 1000 which makes URL very very long. This long URL is not working on the browser because of the URL length limit. It throws HTTP code - 400. We have already tried Chrome, Firefox and Safari. 

Is there any way to shorten this URL at Script Include level. Does ServiceNow platform provide any such utility?

 

11 REPLIES 11

Amit160
ServiceNow Employee
ServiceNow Employee

Hi Allen, Alikutty,

Thank You for kind your feedbacks. 🙂

Our case is slightly different. As per the current scenario, we are creating the URLs in the 'Script Include' and then from the 'Client Script' we are fetching this URL to be embed in an href link. The URLs are being constructed dynamically. So, I am not sure how it will work thru Processors. 

The POST method would again have an issue as it is an href link. Tried some approaches how to use POST not GET Http to invoke the link but failed to do so.

So we can process this URL at two places - either at the 'Script Level' or at the 'Client Script'. Is it possible that we can use some utility to shorten this URL at these levels itself?

Kindly share your thoughts.

Regards,

Amit

Can you explain on what link you are constructing dynamically? And what happens when user clicks on it?

Alikutty A
Tera Sage

I can think of a solution here but you need to try it out.

Have you seen dynamic filters in Service Now? You could construct your own dynamic filter that populates records as per your requirement. For eg:  Assignment Group is (dynamic) One of My groups lists you all the groups where you are a member. The background logic for this dynamic filter is being setup by scripts.

Here is the OOB filter configuration - https://instance_name.service-now.com/nav_to.do?uri=sys_filter_option_dynamic.do?sys_id=d6435e965f51...

find_real_file.png

The encoded query is shortened string 'active=true^assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744'

d6435e965f510100a9ad2572f2b47744 is the SYS_ID of your dynamic filter option and it basically returns an encoded query of the format 'assignment_groupINSYS_ID1,SYS_ID2,SYS_ID3,SYS_ID4,SYS_ID5' where SYS_ID are all my groups

I could place this on the URL as https://instance_name.service-now.com/incident_list.do?sysparm_query=active=true^assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744

and it will filter me with the required records

Similarly you need to create a new dynamic filter option which calls your script include and it returns you required sys id's in it. There are few OOB samples that calls script include eg Get CMDB Item Category, Tabs Reference Qualifier

Or you could refer Create a dynamic filter option

You could then use it in your URL and hopefully it should shorten your URL and work for you. Please try it out and let me know.

 

Amit160
ServiceNow Employee
ServiceNow Employee

Thank You Alikutty,

I will explore the Dynamic Filter Option. 

Meanwhile, we found below function which creates the tiny URL. 

 

function handleLongURLs(event){
var url = event.target.href;
if(url.length <= 1500) {
return true;
}
jQuery.ajax({
type: "POST",
url: "/api/now/tinyurl",
data: JSON.stringify({url : url}),
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function(response) {
window.open(response.result);
});
return false;
}

 

It works for me.

Hi Amit,

Can you please let me know how did you get this working? I am facing the same issue. Thanks in advance.

Thanks.