<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Redirect to servicePortal homepage from your Catalog client script , using confirmation Dialog Box in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/redirect-to-serviceportal-homepage-from-your-catalog-client/m-p/3048583#M899</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;H3&gt;Use Case&lt;/H3&gt;&lt;P&gt;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 &lt;STRONG&gt;No&lt;/STRONG&gt;, they are redirected back to the homepage of the Service Portal. If they choose &lt;STRONG&gt;Yes&lt;/STRONG&gt;, they proceed with the form submission.&lt;/P&gt;&lt;H3&gt;Function to Redirect to the Portal Homepage&lt;/H3&gt;&lt;P&gt;To handle this dynamically without hardcoding the URL, we developed the following function:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 &amp;gt; 0;  // Filter out empty segments
    });

    // Construct the homepage path using the first segment
    var homepagePath = pathSegments.length &amp;gt; 0 ? '/' + pathSegments[0] : '/';

    // Dynamically redirect to the homepage of the current portal
    var homeURL = top.window.location.origin + homepagePath;
    top.window.location = homeURL;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;Explanation:&lt;/H3&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;window.location.pathname&lt;/STRONG&gt;: 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).&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Filtering segments&lt;/STRONG&gt;: 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.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Redirect Logic&lt;/STRONG&gt;: After getting the first segment, we dynamically construct the homepage URL and redirect the user.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;H3&gt;Client Script Implementation&lt;/H3&gt;&lt;P&gt;Here’s how the client script was structured to show the confirmation dialog box and perform the redirection when the user clicks &lt;STRONG&gt;No&lt;/STRONG&gt;:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;How It Works:&lt;/H3&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Confirmation Check&lt;/STRONG&gt;: 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.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Redirect to Portal Home&lt;/STRONG&gt;: If the user chooses &lt;STRONG&gt;No&lt;/STRONG&gt;, they are redirected to the portal homepage dynamically using the redirectToPortalHome() function without hardcoding any URL.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;HR /&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Let me know if you have any questions or suggestions!&lt;BR /&gt;&lt;BR /&gt;Also check out --&amp;gt;&amp;nbsp;&lt;A class="" href="https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/bc-p/3033976#M8936" target="_blank" rel="noopener"&gt;How To: Async GlideAjax in an onSubmit script&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2024 06:56:25 GMT</pubDate>
    <dc:creator>hrawat738405662</dc:creator>
    <dc:date>2024-09-19T06:56:25Z</dc:date>
    <item>
      <title>Redirect to servicePortal homepage from your Catalog client script , using confirmation Dialog Box</title>
      <link>https://www.servicenow.com/community/community-central-forum/redirect-to-serviceportal-homepage-from-your-catalog-client/m-p/3048583#M899</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;H3&gt;Use Case&lt;/H3&gt;&lt;P&gt;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 &lt;STRONG&gt;No&lt;/STRONG&gt;, they are redirected back to the homepage of the Service Portal. If they choose &lt;STRONG&gt;Yes&lt;/STRONG&gt;, they proceed with the form submission.&lt;/P&gt;&lt;H3&gt;Function to Redirect to the Portal Homepage&lt;/H3&gt;&lt;P&gt;To handle this dynamically without hardcoding the URL, we developed the following function:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 &amp;gt; 0;  // Filter out empty segments
    });

    // Construct the homepage path using the first segment
    var homepagePath = pathSegments.length &amp;gt; 0 ? '/' + pathSegments[0] : '/';

    // Dynamically redirect to the homepage of the current portal
    var homeURL = top.window.location.origin + homepagePath;
    top.window.location = homeURL;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;Explanation:&lt;/H3&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;window.location.pathname&lt;/STRONG&gt;: 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).&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Filtering segments&lt;/STRONG&gt;: 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.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Redirect Logic&lt;/STRONG&gt;: After getting the first segment, we dynamically construct the homepage URL and redirect the user.&lt;/P&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;H3&gt;Client Script Implementation&lt;/H3&gt;&lt;P&gt;Here’s how the client script was structured to show the confirmation dialog box and perform the redirection when the user clicks &lt;STRONG&gt;No&lt;/STRONG&gt;:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;How It Works:&lt;/H3&gt;&lt;UL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Confirmation Check&lt;/STRONG&gt;: 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.&lt;/P&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Redirect to Portal Home&lt;/STRONG&gt;: If the user chooses &lt;STRONG&gt;No&lt;/STRONG&gt;, they are redirected to the portal homepage dynamically using the redirectToPortalHome() function without hardcoding any URL.&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;HR /&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Let me know if you have any questions or suggestions!&lt;BR /&gt;&lt;BR /&gt;Also check out --&amp;gt;&amp;nbsp;&lt;A class="" href="https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/bc-p/3033976#M8936" target="_blank" rel="noopener"&gt;How To: Async GlideAjax in an onSubmit script&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 06:56:25 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/redirect-to-serviceportal-homepage-from-your-catalog-client/m-p/3048583#M899</guid>
      <dc:creator>hrawat738405662</dc:creator>
      <dc:date>2024-09-19T06:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: A way to redirect to serviceportal homepage from your client script , using confirmation Dialog</title>
      <link>https://www.servicenow.com/community/community-central-forum/redirect-to-serviceportal-homepage-from-your-catalog-client/m-p/3049535#M910</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 06:16:51 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/redirect-to-serviceportal-homepage-from-your-catalog-client/m-p/3049535#M910</guid>
      <dc:creator>hrawat738405662</dc:creator>
      <dc:date>2024-09-19T06:16:51Z</dc:date>
    </item>
  </channel>
</rss>

