ServiceNow URLs, as we all know, are not the easiest things to look at or work with. With long sys_id values, various other parameters it can be confusing and complicated to share these links around.
Using a very simple UI page you can make these URLs much easier to share around. I have fully commented the code, so if you are new to ServiceNow development hopefully it will be of use to you.
Example URL
https://{instancename}.service-now.com/direct_task.do?sysparm_number=INC0000001
Type: UI Page
HTML
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_url">
// Retrieve the record number passed in the URL Param 'sysparm_number'
var number = RP.getParameterValue('sysparm_number');
// Create a variable to hold our result
var answer;
// GlideRecord query to find the record
var gr = new GlideRecord('task');
gr.addQuery('number', '=', number);
gr.query();
if(gr.next()){
// Found a record with the matching number
answer = gs.getProperty('glide.servlet.uri') + 'nav_to.do?uri=' + gr.sys_class_name + '.do?sys_id=' + gr.sys_id;
answer;
}else{
// Couldn't find a record with the matching number
answer = 0;
answer;
}
</g:evaluate>
<!-- Set the value of a hidden field to the answer from the above g:evaluate for the client script to work with-->
<input type="hidden" name='recurl' id='recurl' value="${jvar_url}" />
<!-- An empty div where we can put an error message with the client script -->
<div style='text-align: center;' id='error_message'>
</div>
</j:jelly>
Client Script
// Get the value of the hidden field into a variable
var answer = document.getElementById('recurl').value;
if(answer == 0){
// A match for the record was not found so fill in the error message
document.getElementById('error_message').innerHTML = '<h2>The specified record was not found</h2><p><a href="/navpage.do">Home</a>';
}else{
// A match was found for the record, redirect to it
window.location = answer;
}
FYI, if you’re starting with an Incident Number (or any record number) and know the table name, you can always structure the URL like the following:
https://instance.service-now.com/TABLE_NAME.do?sysparm_query=number%3DNUMBER
So, to access Incident INC0000001 it would look like:
https://instance.service-now.com/incident.do?sysparm_query=number%3DINC0000001
This should work for any table, but you will obviously have to know the table name being referenced.
-Rob
That’s good to know Rob, thanks.
Yes, the use case is one where the there is no need to know the tables class in order to get to a record.
Thanks for reading.
Callum