1

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);
 }
}
  • What are your getToken() and triggerEvent() returning? Could they be returning undefined or raising an error leading you to always ending up at the error page? – Macca Jul 24 at 5:47
  • If I remove the error page redirect, and replace with write, it works fine. When it is success, it will redirect to success page and error is displayed on the page. Problem is only when I add the 2 error page redirects. – chrissyj13 Jul 24 at 5:51
  • Can you try flipping the second param of your Redirects from "true" to "false" and clear your cache/bypass the cache in developer tools? You're setting a permanent redirection (301) that would be cached by your browser. – Macca Jul 24 at 6:10
  • hey @Macca, doesn't seem to work. Still sends to error page. :( – chrissyj13 Jul 24 at 6:37
  • 1
    Redirect function inside try throws an error - reffer to this respons by Gortonington - salesforce.stackexchange.com/questions/311799/… – Mateusz Bartkowiak Jul 24 at 12:07
3

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.

| improve this answer | |
  • This worked! Thanks so much! – chrissyj13 Jul 27 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.