Debugging AMPscript

Debugging AMPscript in Salesforce Marketing Cloud can be a pain, as there is no built-in feature that would show script errors in CloudPages. The idea to add a debugging feature to CloudPages has been hanging in the Traiblazer Community’s “Ideas” section for two years now and haven’t yet reached the point threshold set by Salesforce – so if you’re reading this, visit the Add Debugging Support for CloudPages site and give it a thumb up!

Debugging AMPscript in CloudPages

When you’re working on a script in a CloudPage, the first indication that there is something wrong is the fact that when you try to publish your page, you never get a preview and the throbber just keeps spinning:

If you publish the page anyway and later try to access it, you will get the infamous “500 – Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.” message without any indication of what went wrong:

To get some details about the error behind this, try wrapping your AMPscript with a Server-Side JavaScript try/catch block:

<script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>
%%[ your AMPscript block goes here ]%%
<script runat="server">
}catch(e){
Write(Stringify(e));
}
</script>

Here is a snippet of an actual script:

<script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>
%%[
set @email = emailaddr
if (@email != "") then
set @rows = LookupRows("LeadDataExtension","Email", @email)
set @rowCount = rowcount(@rows)
if @rowCount > 0 then
set @row = Row(@rows, 1)
set @FirstName = Field(@row,"FirstName")
endif
endif
]%%
<script runat="server">
}catch (e) {
Write("<b>Error Message:</b> " + Stringify(e.message) + "<br><br><b>Description:</b> " + Stringify(e.description));
}
</script>
Hello %%=v(@FirstName)=%%!

This will allow you to catch some of the possible errors related to your AMPscript – here are some example error messages:

Error Message: "Call to create the salesforceobject Contact failed! Error status code: REQUIRED_FIELD_MISSING\nError message: Required fields are missing: [LastName]"

Description: "ExactTarget.OMM.FunctionExecutionException: Call to create the salesforceobject Contact failed! Error status code: REQUIRED_FIELD_MISSING\nError message: Required fields are missing: [LastName]\r\n Error Code: CREATESFOJBECT_FUNC_ERROR\r\n - from Jint\r\n\r\n"

As you can see from the above example, the error message and description will give you a clue which AMPscript function is causing the problems.

If you add a console log to the try/catch script, you will be able to see the message in your browser’s console, while it will remain hidden from the subscriber visiting your page:

<script runat="server">
Platform.Load("Core","1.1.1");
try{
</script>
%%[ your AMPscript block goes here ]%%
<script runat="server">
}
catch(e) {
Variable.SetValue("errorMessage", Stringify(e.message) + Stringify(e.description));
}
</script>
<script runat="client">
console.log(`%%=v(@errorMessage)=%%`);
</script>

Now when you open your CloudPage, you will be able to see the error message when you access the console (press F12 in Chrome or press Command+Option+C on Mac or Control+Shift+C on Windows):

Debugging with the try/catch statement will show you some errors related to the functions you used or if a record referenced in your lookup function wasn’t found in the Data Extension. Unfortunately, you will still get the 500 error in other cases, for example, if you are referencing a non-existing Data Extension in your lookup function, or if you are trying to query a parameter which hasn’t been passed correctly. In that case, the best thing you can do is to go through your script line by line, and display all the variables in your script one by one, until you find the function which is not resolving correctly, here is an example:

%%[
set @email = emailaddr
if (@email != "") then
set @rows = LookupRows("LeadsDataExtension","Email", @email)
set @rowCount = rowcount(@rows)
if @rowCount > 0 then
set @row = Row(@rows, 1)
set @UID = Field(@row,"UID")
endif
endif
]%%
<span style="color: green">@email:</span> %%=v(@email)=%%
<span style="color: green">@rowCount:</span> %%=v(@rowCount)=%%
<span style="color: green">@UID:</span> %%=v(@UID)=%%
@email: test@test.com
@rowCount: 0
@UID:

You can also use the Output function to output nested functions at the location where the code block appears in your CloudPage, without having to output them inline later using %%=v()=%%. Remember that it will only output a nested function, and it won’t work if you try to resolve a single variable, for example Output(@UID).

One last thing worth mentioning is to always remember that the purpose of AMPscript is to personalize a CloudPage for each subscriber, so you cannot expect your CloudPage to resolve correctly from the CloudPage link in the editor. You will always need to pass the required parameters used in your script, either by adding them to the URL (?myparam1={id1}&myparam2={id2}) or by creating a link or a button in an email using the CloudPagesURL function, and previewing that email against a subscriber.

Debugging AMPscript in Email Studio

It is a little bit easier to debug AMPscript in Email Studio because the system will show you the errors as soon as you try to preview the email against a subscriber. It is also a bit faster because you will see the results on-screen, without the need to publish the changes every time. Unfortunately, not all AMPscript functions will work in an email, but in general, it’s good for the cases when you are trying to find a bug in your script and have to go line by line.

Note that there are two separate error messages – for the HTML version and the Text version of your email. It often happens, that you copy an existing email to keep a consistent design, and while you make changes to the HTML version, you forget to update the text version of your email. This can lead to errors, with outdated AMPscript functions hidden in the text version.

Online tools for validating AMPscript

Although not meant for debugging, but rather for checking whether the sytax of your script is correct, ampscript.io will validate your script and show you real-time tips that will help you fix possible errors in your script. Syntax highlighting will also make it easier for you to edit and troubleshoot your script. ampscript.io is free to use and doesn’t require any downloads or registration:

AMPscript best practices

Always remember to follow the best practices and wherever possible, use blocks of AMPscrip. Make comments in your script so that it’s easy to read when someone else has to take over from you and use indentation. Use the RaiseError function in emails and crate rules for exception handling in CloudPages.

If you are looking for more resources on AMPscript debugging and best practices, check out the ampscript.guide and the official documentation.

7 thoughts on “Debugging AMPscript

  1. Rohan Kulkarni

    Hi Zuzanna,

    I am newbie to coding. can you please let me know what does “Server-Side JavaScript try/catch” mean and how do I use it on my laptop?

    This would really be helpful as there is no convenient way to catch errors in AMPscript. Thanks in advance!!

    Liked by 1 person

    1. Hi Rohan! The role of try/catch is as follows:
      try: The code block that follows this statement will contain the code to try and see whether it gives an error
      catch: The block that follows this statement will see if the code following the try statement gave an error, and decides what to do with it.
      To use try/catch to debug AMPscript, you need to wrap it around your AMPscript blocks. Here is an example of how you could use it on a CloudPage: https://gist.github.com/zuzannamj/08fe8d5b4f99d32b21ff3347515c1923

      Like

  2. Jeni H.

    Hi Zuzanna! I just wanted to thank you for the reminder about updating the text version when copying an existing email, that literally just saved the day at my work after several of us spent days trying to fix an issue in the preview screen.

    Like

Leave a comment