Error Handling with Try-Catch Block in SSSJ and Ampscript

Author: Florin Valean

Use case

Try-Catch block is very useful as it allows programmers to handle errors and exceptions in their code. It can generate a more detailed and descriptive error message to help with troubleshooting and debugging and provides the means for replacing very specific server error messages with messages that are user-friendly and easier to understand. The syntax for a try-catch block is this:

try { /* code A */ } catch(e) { /* code to execute when an error/exception is thrown during the execution of code A */ }

Server-side javascript sample code

You can create a landing page in Web Studio and include this code. Publish the page and load it in a web browser to test the code.

/* replace the highlighted values with yours */ <script runat="server"> Platform.Load("Core", "1"); try { /* get OAuth 2.0 access token */ var payload = '{"grant_type": "client_credentials",'; payload += '"client_id": "d20aaul96htb2hgo9b4i47gx",'; payload += '"client_secret": "tUriuDS0zdV82vFrLIbJek77",'; payload += '"scope": null,'; payload += '"account_id": "012345678"}'; var OAuth2URL = ""; var contentType = 'application/json'; var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload); var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]); var accessToken = tokenObj.access_token; Write(accessToken); } catch(e) { Write(Stringify(e)); } </script>

The code presented above makes a RESP API HTTP Post request to get an access token from Marketing Cloud. Everything happens in the try block. If there is any error thrown in the try block then this error is captured in the catch block and the error message is displayed. Notice the use of Stringify function which is used to convert an object to a string in order to display an intelligible message.

Combine SSJS & Ampscript

Ampscript code can be included in a try-catch block. End the javascript code and include the ampscript block; resume the javascript block after the ampscript block as shown below:

/* replace the highlighted values with yours */ <script runat="server"> Platform.Load("Core", "1"); try { </script> /* ampscript code */ %%[ var @email, @firstName, @numberOfRowsInserted set @email = "" set @firstName = "Jane" set @numberOfRowsInserted = InsertData('MyDataExtensionName', 'Email', @email, 'FirstName', @firstName, 'CreatedDate', NOW()) ]%% <script runat="server"> var rowsInserted = Variable.GetValue("@numberOfRowsInserted"); Write("Number of rows inserted: " + rowsInserted); } catch(e) { Write(Stringify(e)); } </script>

The ampscript code is included in the pan try block and any error thrown during the execution of this ampscript code will be captured and treated in the pan catch block. If successful, the ampscript code will insert a row in a data extension called MyDataExtensionName and the javascript code will display the number of rows inserted.
Notice the use of Variable.GetValue function which is used to get a value from an ampscript variable into a javascript variable. There is also a Variable.SetValue function in server-side javascript which can pass a value from a javascript variable to an ampscript variable.

Share this page
Stay in touch

Subscribe to the newsletter

p1 p2 p3