Redirecting to Task Records from Custom URLs - ben-vargas/servicenow-wiki GitHub Wiki
This article explains how to create a ServiceNow UI Page that redirects users to a specific Task record when accessed via a custom URL, using a query parameter. This can be useful for creating shareable links or integrating with other systems.
Sometimes, you may need to provide users with a custom URL that directly links to a specific task in ServiceNow. Instead of relying on the standard record URL, which includes the table name and sys_id, you might want to use a simpler URL that includes a unique identifier like a task number. This approach requires extracting the task number from the custom URL and using it to redirect users to the actual task record.
This solution creates a custom UI Page that will automatically redirect to the appropriate task record using the nav_to.do
endpoint. The UI Page will extract a task number from the URL parameter and use it to construct a new URL that points to the correct task record.
This solution involves the following steps:
- Create a UI Page: Create a custom UI Page that includes the JavaScript for URL parsing and redirection.
- Accessing the Custom URL: Provide a custom URL that includes the task number to use for the redirect.
Code Snippets:
Here's the UI Page code:
<?xml version='1.0' encoding='utf-8' ?>
<j:jelly trim='false' xmlns:j='jelly:core' xmlns:g='glide' xmlns:j2='null' xmlns:g2='null'>
<script language='javascript'>
addLoadEvent(overrideView);
function overrideView() {
var inURL = location.href.toString();
var numberArray = inURL.split('?');
var number = numberArray[numberArray.length - 1];
var outURL = 'nav_to.do?uri=task.do?sysparm_query=number=' + number;
window.location = outURL;
}
</script>
</j:jelly>
Explanation:
-
addLoadEvent(overrideView);
:- This ensures that the
overrideView()
function is executed when the UI Page is loaded.
- This ensures that the
-
overrideView()
Function:-
var inURL = location.href.toString();
retrieves the current URL. -
var numberArray = inURL.split('?');
splits the URL into an array based on the?
character, which separates the main URL from the query parameters. -
var number = numberArray[numberArray.length - 1];
extracts the last element from the array. This is assuming the last parameter in the URL is going to be the task number. -
var outURL = 'nav_to.do?uri=task.do?sysparm_query=number=' + number;
constructs a new URL usingnav_to.do
, which allows you to navigate to a ServiceNow record using auri
parameter. Thesysparm_query
parameter is used to search all records in thetask
table using the task number extracted from the original URL. -
window.location = outURL;
redirects the user to the new URL, taking them directly to the task record in ServiceNow.
-
-
Create a UI Page:
- Navigate to System UI > UI Pages.
- Create a new UI Page with a name (e.g.,
task_redirect
). - Copy the code from the "Code Snippets" section above into the HTML field of the UI Page.
-
Custom URL: Access the UI page using a custom url with the task number as the last parameter.
https://<your-instance>.service-now.com/task_redirect?TASK0000000
- Error Handling: The provided script does not include error handling. Consider adding error handling to handle situations where the URL does not contain a task number or when the task number doesn't match a record.
- Performance: When using this in production, test and monitor the performance to see if there are any negative impacts from the lookup of the record.
- Security: While this example doesn't add extra security considerations, ensure that you are using this method correctly and not exposing sensitive information.
- Parameter Validation: Add validation to ensure that the URL parameter exists, and the correct format is used.
- Multiple Parameters: Modify the code to handle multiple parameters if needed.
- Table Flexibility: Enhance the UI Page to accept a table parameter so it can redirect to records in multiple tables (e.g. Incident, Change, etc.).
- Error Messaging: Add logic to display a user friendly message if the redirect fails.
-
Direct Sys ID lookup: Instead of using the
task.do
endpoint, you could useform.do
, if you have the sys_id. This would be more performant than querying based on task number.
Example Enhanced Code:
<?xml version='1.0' encoding='utf-8' ?>
<j:jelly trim='false' xmlns:j='jelly:core' xmlns:g='glide' xmlns:j2='null' xmlns:g2='null'>
<script language='javascript'>
addLoadEvent(overrideView);
function overrideView() {
var urlParams = new URLSearchParams(window.location.search);
var number = urlParams.get('number');
var table = urlParams.get('table');
var outURL;
if (number && table) {
outURL = 'nav_to.do?uri='+table+'.do?sysparm_query=number=' + number;
window.location = outURL;
} else {
// Handle the case where number or table is missing or invalid
alert("Invalid or Missing Task Number or Table. Please check your URL");
}
}
</script>
</j:jelly>
Using the enhanced code
- You can call the UI page with the parameters like this:
https://<your-instance>.service-now.com/task_redirect?number=TASK0000000&table=incident
https://<your-instance>.service-now.com/task_redirect?number=CHG0000000&table=change_request
- Clear Naming: Use descriptive names for your UI Pages to help with maintainability.
- Error Handling: Always add error handling to account for missing or invalid URL parameters.
- Performance: Test thoroughly to ensure this approach doesn't negatively impact performance.
- Security: Be careful with exposing any sensitive information through the URL.
- Comments: Add comments to your code to explain your logic.
This article has shown how to create a custom UI Page that redirects users to a Task record based on a task number provided in the URL parameters. By parsing the URL and using the nav_to.do
endpoint, you can create a custom navigation method that can be used for various use cases within and outside of ServiceNow. By using the enhanced code, you can provide more flexibility to users by allowing them to pass in a task number and a table. Remember to test all changes thoroughly in a non-production instance before deploying to production.