Error When Redirecting to Standard Ticket Page onSubmit Catalog Item

appstorm
Tera Contributor

I am attempting to redirect users to our standard ticket page (instead of the REQ confirmation page) after submitting a request from one of our Catalog Items in Service Portal.  However, I consistently get this error:

 

"Unhandled exception in GlideAjax. Cannot read properties of null (reading 'location')"

 

I've read about VA's and security restrictions that may prevent catalog redirects onSubmit.  However, I am going to include my code with hopes someone may know a way around this.

 

CS

function onSubmit() {
    var sysId = g_form.getUniqueValue();  // Get the sys_id of the catalog item
    var table = 'sc_req_item';  // Ensure the correct table name for your catalog item

    // Use GlideAjax to call the Script Include
    var ga = new GlideAjax('CatalogRedirector');  // Name of your Script Include
    ga.addParam('sysparm_name', 'getRedirectUrl');  // Method to call in Script Include
    ga.addParam('sys_id', sysId);  // Pass the sys_id
    ga.addParam('table', table);  // Pass the table name

    // Make the GlideAjax call
    ga.getXMLAnswer(function(response) {
        // Log the raw response to check its value
        console.log("Raw response:", response);

        // Check if the response is empty or null
        if (!response) {
            console.error("Error: No response returned from GlideAjax.");
            return; // Prevent further execution if no response is received
        }

        // Attempt to extract the redirect URL from the response
        var redirectUrl = response;

        // Debug: Check if the URL is valid
        console.log("Redirect URL: " + redirectUrl);

        if (redirectUrl) {
            console.log("Redirecting to: " + redirectUrl);
            // Use window.location.replace to force the redirect
            window.location.replace(redirectUrl);  // This might work better in some environments
        } else {
            console.error("Error: No redirect URL returned.");
        }
    });

    // Prevent the form from submitting immediately (so we can handle the redirect first)
    return false;
}

 

SI

var CatalogRedirector = Class.create();
CatalogRedirector.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    // Client Callable function to retrieve the redirect URL
    getRedirectUrl: function() {
        var sysId = this.getParameter('sys_id');
        var table = this.getParameter('table');

        // Validate parameters
        if (!sysId || !table) {
            gs.info('Error: Missing sys_id or table');
            return 'Error: Missing sys_id or table parameter'; // Return error message if parameters are missing
        }

        // Construct the redirect URL (replace this logic with your own URL logic)
        var redirectUrl = 'https://...standard_ticket&table=' + table + '&sys_id=' + sysId;

        // Log the redirect URL to ensure it's correct
        gs.info('Redirect URL: ' + redirectUrl);

        // Return the redirect URL as the answer
        return redirectUrl;
    }
});

 

1 REPLY 1

J Siva
Tera Sage

Hi @appstorm 

I don't think it's possible to achieve this using client callable script include and on submit client script.

Since it's asynchronous, it won't work as expected.

Please checkout the below community post which suggests cloning and modifying the OOB portal widget.

https://www.servicenow.com/community/developer-forum/how-to-redirect-user-to-ritm-after-submitting-c...

 

Regards,

Siva