Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

SFMC TIP | Redirect error inside try catch

SFMC tips and tricks

If you have Redirect function inside the try catch block, either as mixture of SSJS with AMPScript or only in SSJS, it will throw an 500 – Internal Server Error.

Let’s take a look on few examples what can create an error that will take you some quality debugging time to figure it out. And frankly to debug something inside the marketing cloud is one of the most depressing things happened to me in a while.

<script runat=server>
try{
  //do somothing
  throw 'This is error'
}catch(e){
	Redirect('https://example.com',true)
}

</script>
<script runat=server>
 try{
 </script>
  %%[
   /* Do some AMPScript */
   /* e.g. */
 	UpdateSingleSalesforceObject('Contact', 'contactId', 'someField', 'True')
    Redirect(CloudPagesURL(1))
 ]%% 
 <script runat=server>
 }catch(e){
  //do something
 }
</script>

Examples above will throw beautiful 500 – Internal Server Error and leave you guessing what is wrong this time.

To make this work you have to move any redirect functions outside the try-catch block.

<script runat=server>
 var error = false
try{
  //do somothing
  throw 'This is error'
}catch(e){
  	/* you can log e.message into  Data Extension */
	error = true
}
Redirect('https://example.com', true)
</script>

Oh hi there đź‘‹
I have a FREE e-book for you.

Sign up now to get an in-depth analysis of Adobe and Salesforce Marketing Clouds!

We don’t spam! Read our privacy policy for more info.

Leave a comment

Your email address will not be published. Required fields are marked *