TransWikia.com

SSJS redirecting to wrong page

Salesforce Asked on December 24, 2021

I have a processing cloud page and the page redirects correctly to success page but when I add in extra bit of code for error page redirect, records evaluating success still goes to the error page. Am I missing something here?

Thank you in advance.

try {
   var token = getToken(setup);
   var success = false
   if (!!token) success = triggerEvent(token setup, data);
   if (!!success) {
      Redirect("https://successpage.com", true);}
   else {
      Redirect("https://errorpage.com", true);}
} catch (e) {
  Redirect("https://errorpage.com",true);
 }
}

One Answer

This is an issue around that all redirect functions will throw an exception due to the 'breaking' from the script. There are a couple ways to get around this, please see this SFSE post that recently experienced the same issue or check out a post I made on HowToSFMC.com about this as well as other potential pitfalls with Try/Catch.

Basic synopsis of the issue:

It appears this is a well documented .Net issue revolving around that Redirects will throw a ‘ThreadAbort’ exception, which would trigger the catch block to run instead. Now SFMC SSJS does not have all the great exception handling capabilities that you can see in languages like .Net.

and my recommended solution:

<script runat="server">
    var redirect = 'https://google.com'

    try{
          Platform.Response.Redirect(redirect)
    } catch(e) {
        var desc = e.description; //Pulls the description from error object
        if(desc.indexOf("ExactTarget.OMM.AMPScriptRedirectException") > -1) {
          Platform.Response.Write(desc) //This is arbitrary as will not be run
        } else {
          redirect = 'https://yahoo.com'
          Platform.Response.Redirect(redirect)
        }
    }
</script>

By throwing the redirect into an if statement, you remove it from being processed if the exception thrown is just from the redirect, it will instead only be processed for other exceptions.

Answered by Gortonington on December 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP