URL redirection from one url to another

Karada Sagar Pa
Kilo Guru

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.

3 REPLIES 3

Shaqeel
Mega Sage

Hi @Karada Sagar Pa 

 

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

This is not working 

 

(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 !

Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj