๐Ÿ†• How to debug AMPScript Code

Learn how to debug code like a pro, the right way (?

ยท

5 min read

Hi Trailblazers!

It's been a while since I last wrote to you... but today I bring you the solution to all your problems (well, not all of them ๐Ÿ˜ฅ)

Many times, when the code we write generates an error at runtime, we cannot see what the error was or what went wrong. This can be stressful if we are in a time-sensitive project and we must deliver the feature in record time.

Today we are going to see one of the easiest ways to find a syntax error in AMPScript or SSJS, as well as how to avoid a "500 - Internal Server Error" screen. It also works for runtime errors, such as adding a number to a letter.

Let's get started

To catch this error, you need to create two blocks. A container block and one that will have the actual code that is giving you problems. These can be created in Content Builder as Code Snippets.

Code Snippet with your original code, the one that usually fails:

Name & ExternalKey: code-block

%%[ 

var @hello
set @world = "ok? 
// Yes, a quotation mark is missing

]%%

Now let's see the code we will use inside the cloudpage. In order not to overwrite several times the same cloudpage, we will use a reference to this next block:

Name & ExternalKey: cloudpage-block

<script runat="server">
try {
  Platform.Response.Write(
    Platform.Function.ContentBlockByKey("code-block")
  );
} catch(e) {
  var exc = e.message;
  if (e.description){ exc = exc + '\n' + e.description; }
  if (e.jintException){ exc = exc + '\n' + e.jintException; }
  Platform.Response.Write(exc);
}
</script>

If you used the above names, now in the cloudpage we are going to write the following and publish it:

%%=ContentBlockByKey("cloudpage-block")=%%


Syntax Error

The code of the first block certainly has a flaw, it is missing a quotation mark (a syntax error generated on purpose).

When creating the cloudpage, I am using a Code Resource TEXT type (it has some advantages that we will see in a future article).

Let's see what SFMC shows us when we open the cloudpage:

An error occurred when attempting to evaluate a ContentBlockByKey function call.
  Function Call: ContentBlockByKey("code-block")
  See inner exception for details.<hr>ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a ContentBlockByKey function call.
  Function Call: ContentBlockByKey("code-block")
  See inner exception for details.
  Error Code: OMM_FUNC_EXEC_ERROR
 - from Jint --> 

 --- inner exception 1---

ExactTarget.OMM.OMMException: An error occurred when attempting to parse HtmlEmailBody content for HTML content.
  MemberID: XXXXXXXX
  JobID: 0
  ListID: 0
  Content Begins With: %%[ 

var @hello
set @world = "ok? 
// Yes, a quotation mark is missing

]%%
  Error Code: OMM_VAR_SET_EXPR_INVALID
 - from OMMCommon --> 

 --- inner exception 2---

ExactTarget.OMM.InvalidScriptException: The SET variable value expression is invalid.  See inner exception for detail.
  Script: set @world = "ok? 
// Yes, a quotation mark is missing

]%%
  Variable Name: @world
  Index: 20
  ListID: 0
  Error Code: OMM_VAR_SET_EXPR_INVALID
 - from OMMCommon --> 

 --- inner exception 3---

ExactTarget.OMM.InvalidScriptException: An error occurred when attempting to resolve a script expression. See inner exception for detail.
  Script Expression:  "ok? 
// Yes, a quotation mark is missing

]%%
  MemberID: XXXXXXXX
  JobID: 0
  Error Code: OMM_SCRIPT_SYNTAX_ERR
 - from OMMCommon --> 

 --- inner exception 4---

ExactTarget.OMM.InvalidScriptException: The quoted expression value does not include a closing quote.
  Script Expression:  "ok? 
// Yes, a quotation mark is missing

]%%
  MemberID: XXXXXXXX
  JobID: 0
  Error Code: OMM_SCRIPT_SYNTAX_ERR
 - from OMMCommon

Eureka! Now we can detect following a "Stack trace" that shows us the error message of the main exception:

ExactTarget.OMM.InvalidScriptException: The quoted expression value does not include a closing quote. Script Expression: "ok?

It also gives us an idea of how the classes are structured a bit in the backend, showing "ExactTarget.OMM.InvalidScriptException".

Now let's correct the block code:

%%[ 

var @hello
set @world = "world!"

]%%

We saved the changes to the block and, since we didn't have to update the cloudpage but the block in reference, there is no need to publish again!

Depending on where you are geographically located, sometimes publishing or updating a cloudpage can take a few minutes for the changes to be online on the internet. This applies to HTML/CSS FrontEnd code as well as AMPScript/Backend code.

The cloudpage will not show anything but when we see the blank screen we realize that the AMPScript was executed correctly during the page load.


Runtime Error

Let us now look at a different error, which occurs during code execution.

Let's change the code to the following:

%%[ 

set @result = Add(1,"a")

]%%

The above code performs an addition and it will fail because (at least where I live) you cannot add 1 + "a".

Let's see the result:

An error occurred when attempting to evaluate a ContentBlockByKey function call.
  Function Call: ContentBlockByKey("code-block")
  See inner exception for details.<hr>ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a ContentBlockByKey function call.
  Function Call: ContentBlockByKey("code-block")
  See inner exception for details.
  Error Code: OMM_FUNC_EXEC_ERROR
 - from Jint --> 

 --- inner exception 1---

ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate an Add function call.  See inner exception for details.
  Error Code: OMM_FUNC_EXEC_ERROR
 - from OMMCommon --> 

 --- inner exception 2---

ExactTarget.OMM.InvalidFunctionParameterException: Invalid value specified for function parameter.
  Function Name: Add
  Parameter Name: Value2
  Parameter Ordinal: 2
  Parameter Type: Numeric
  Submitted Value: a
  ClientID: XXXXXXXXX
  JobID: 0
  ListID: 0
  BatchID: 0
  SubcriberID: 0
  Data Source Type: List
  Error Code: OMM_INVALID_FUNCT_PARAM
 - from OMMCommon

This time we are seeing an execution error when we send an invalid parameter in the function.

ExactTarget.OMM.InvalidFunctionParameterException

If you copy the above code directly into the cloudpage, you will see the amazing and unique error screen that SFMC can give you when you open it:


Conclusion

Today we were able to see the different ways to generate an error and handle it properly.

As a proof of concept, we are simply showing the error message to the public (we wouldn't want to show that to our end customers) so we could add some enhancements like saving the result in a Data Extension, sending a notification Email to the developer in case of error, or simply redirecting the user to a beautiful "Something went wrong" page. It's up to you!

Don't forget to sign up with your email to receive notifications of new articles.
You can also leave me a like or a comment if you liked the article. Let me know if you have any questions.

See you soon and have a nice week โœจ

ย