URL redirection from one url to another
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2024 10:36 PM
Hi All,
We have added one of catalog item sysid and other url data to construct an url for an item to open in servicenow the moment an user clicks it in another 3rd party apps. This url exactly matches with catalog item url only in the other website.
But the moment the user clicks it then the website adds more data to end of the url like loggedin user and other info then it redirects to servicenow. as the url will have some other texts at the end that's why it will not land nowhere. so can anyone please help me here how we can redirect the url again in servicenow so that it can land in proper item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 04:47 AM
You can achieve this by creating a Scripted REST API in ServiceNow.
1. Create a Scripted REST API in ServiceNow.
2. Define the API name, version, and base path.
3. Create a resource under the API.
4. Define the HTTP method (GET) and the Script.
5. In the script, parse the URL parameters and remove the extra parameters that are added by the third-party application.
6. After parsing the URL, redirect to the correct catalog item using the sys_id.
function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var sys_id = request.queryParams.sys_id.toString(); // get sys_id from URL
var redirect_url = "/sc_cat_item.do?sys_id=" + sys_id; // construct the redirect URL
response.sendRedirect(redirect_url); // redirect to the catalog item
})(request, response);
Note: Replace 'sys_id' with the actual parameter name that you are using in your URL.
Mark Helpful/Solution 🙂
Regards
Shaqeel
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 08:18 AM
This is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024 11:16 AM
(function process(request, response) {
// Extract the catalog item Sys ID from the query parameter
var sys_id = request.queryParams.sys_id;
if (!sys_id) {
// Handle the case where the sys_id is missing
response.setError(400, "Missing 'sys_id' parameter.");
return;
}
// Validate the Sys ID by checking if it exists in the 'sc_cat_item' table
var gr = new GlideRecord('sc_cat_item');
if (gr.get(sys_id)) {
// Construct the catalog item URL
var redirect_url = "/com.glideapp.servicecatalog_cat_item_view.do?sys_id=" + sys_id;
// Set the HTTP redirect response
response.setStatus(302); // HTTP status for redirection
response.setHeader('Location', redirect_url); // Redirect to the correct URL
} else {
// Handle the case where the catalog item does not exist
response.setError(404, "Catalog item not found.");
}
})(request, response);
Note - Ensure that the external application calls this API with the correct sys_id.
For example:
Mark Helpful if it helped !
Thanks,
Raj