SFMC tip #3 – redirect error in try catch block

in  Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks
1 minute read
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>

Leave a comment

Your email address will not be published.