Redirect to servicePortal homepage from your Catalog client script , using confirmation Dialog Box

hrawat738405662
Tera Contributor

Hi everyone,

I wanted to share a simple yet effective approach to redirect a user to the Service Portal homepage dynamically from a catalog item form view after they interact with a confirmation dialog box. This can be useful when you want to confirm certain actions, such as submitting a form or making an important decision in the service portal environment.

Use Case

In our case, we were checking whether the user had certain assets assigned and still under warranty before allowing them to proceed with ordering a new laptop or device. If the user already had assigned assets under warranty, a confirmation dialog box would pop up asking if they still want to proceed. If the user chooses No, they are redirected back to the homepage of the Service Portal. If they choose Yes, they proceed with the form submission.

Function to Redirect to the Portal Homepage

To handle this dynamically without hardcoding the URL, we developed the following function:

 

 

 

function redirectToPortalHome() {
    // Get the current path
    var currentPath = top.window.location.pathname;

    // Split the path by "/" and get the first segment (ignoring empty segments)
    var pathSegments = currentPath.split('/').filter(function (segment) {
        return segment.length > 0;  // Filter out empty segments
    });

    // Construct the homepage path using the first segment
    var homepagePath = pathSegments.length > 0 ? '/' + pathSegments[0] : '/';

    // Dynamically redirect to the homepage of the current portal
    var homeURL = top.window.location.origin + homepagePath;
    top.window.location = homeURL;
}

 

 

 

Explanation:

  1. window.location.pathname: This retrieves the path of the current URL, which will return the base path for the Service Portal (e.g., /serviceportal or /your_custom_portal).

  2. Filtering segments: We split the path by / and filter out any empty segments to correctly identify the first part of the path (the portal name). This ensures we are not hardcoding any URL or portal name.

  3. Redirect Logic: After getting the first segment, we dynamically construct the homepage URL and redirect the user.

Client Script Implementation

Here’s how the client script was structured to show the confirmation dialog box and perform the redirection when the user clicks No:

 

 

 

function onSubmit() {
    // If the confirmation has already been shown, allow the form to submit
    if (g_form.getValue('u_confirm_checked') === 'true') {
        g_form.setValue('u_confirm_checked', 'false'); // Reset the flag for future submissions
        return true;
    }

    // Check if the user has assets under warranty
    var hasAssetWithWarranty = g_form.getValue('has_asset_with_warranty');
    
    // Show confirmation if assets are under warranty
    if (hasAssetWithWarranty === 'true') {
        var confirmMessage = 'You already have assets under warranty. Do you want to proceed?';
        var userConfirmed = confirm(confirmMessage);

        // If user clicks "No", redirect to the homepage
        if (!userConfirmed) {
            redirectToPortalHome();  // Call the redirect function
            return false;  // Stop the form submission
        }
        
        // Set the confirmation checked flag so it doesn’t pop up again
        g_form.setValue('u_confirm_checked', 'true');
    }

    // Proceed with the form submission
    return true;
}

 

 

 

How It Works:

  • Confirmation Check: We use g_form.getValue('u_confirm_checked') to ensure the confirmation dialog only shows once per form submission. If the user confirms their choice, the flag is set to true, and the dialog won't appear again.

  • Redirect to Portal Home: If the user chooses No, they are redirected to the portal homepage dynamically using the redirectToPortalHome() function without hardcoding any URL.


This approach ensures the flexibility of dynamically handling redirection based on the current portal URL without hardcoding anything. It also adds a clean user experience with a confirmation dialog that prevents unnecessary form submissions when the user decides not to proceed.

Let me know if you have any questions or suggestions!

Also check out --> How To: Async GlideAjax in an onSubmit script

 

1 REPLY 1

hrawat738405662
Tera Contributor

Note that the above solution is for a catalog client script , when we are on the catalog item form and want to redirect to ServicePortal homepage from that form on click of a button. In our case a on the button press inside the confirmation dialog box.