Optimizing Marketing Operations: Implementing Effective Error Logging in Salesforce Marketing Cloud

Error logging refers to the practice of capturing and recording information about errors or failures that occur during processes or activities within the platform. It involves storing relevant details about the error, such as error messages, timestamps, subscriber information, journey or automation names, activity names, error codes, and other contextual information, in a designated log for analysis and troubleshooting purposes.


Error logging helps in troubleshooting and issue resolution, monitoring, performance optimization, Compliance, Auditing, etc...


Setting up Error Log:

To set up error logging in Salesforce Marketing Cloud, you can follow these steps:


Step 1: Create a Data Extension: 

Start by creating a new Data Extension in Marketing Cloud. This Data Extension will be used to store the error log data. Define the required fields, such as error message, timestamp, subscriber key, email address, etc., based on your specific logging needs.


Step 2: Capturing errors:

SSJS catch block:

A catch block is a code block used in conjunction with a try block to handle exceptions or errors that may occur during the execution of code. The catch block is used to capture and manage these exceptions, allowing for more robust error handling and graceful error recovery. In this context, we will use a catch block to log the errors.

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script runat="server">
    Platform.Load("core", "1.1.5");
 
    function logErrorToDataExtension(errorMessage, subscriberKey, emailAddress, journeyName, activityName, errorCode, source) {
      var logDEName = "ErrorLog"; // Replace with your Data Extension name
 
      // Create a Data Extension row with error details
      var errorLogRow = {
        "Error_Message": errorMessage,
        "Timestamp": Platform.Function.FormatDate(new Date(), "yyyy-MM-dd HH:mm:ss"),
        "Subscriber_Key": subscriberKey,
        "Email_Address": emailAddress,
        "Journey_Name": journeyName,
        "Activity_Name": activityName,
        "Error_Code": errorCode,
        "Source": source
      };
 
      try {
        var logDE = DataExtension.Init(logDEName);
        logDE.Rows.Add(errorLogRow);
      } catch (ex) {
        // Handle any errors encountered while logging the error itself
        Write("Error logging the error: " + ex);
      }
    }
 
    // Example usage within a catch block
    try {
      // Code that may throw an error
    } catch (ex) {
      var errorMessage = ex.message;
      var subscriberKey = "exampleSubscriberKey";
      var emailAddress = "example@example.com";
      var journeyName = "Example Journey";
      var activityName = "Example Activity";
      var errorCode = ex.errorCode;
      var source = "Email"; // Example source value
 
      // Log the error to the Data Extension
      logErrorToDataExtension(errorMessage, subscriberKey, emailAddress, journeyName, activityName, errorCode, source);
    }
</script>

AMPscript Raise Error: 

Here's an example of how you can use AMPscript's RaiseError function to raise an error and insert data into an error log Data Extension:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
%%[
VAR @errorLogDE, @errorMessage, @subscriberKey, @emailAddress, @journeyName, @activityName, @errorCode, @source
  
SET @errorLogDE = "ErrorLog" /* Replace with your Data Extension name */
  
/* Set error details */
SET @errorMessage = "An error occurred."
SET @subscriberKey = AttributeValue("SubscriberKey")
SET @emailAddress = AttributeValue("EmailAddress")
SET @journeyName = "Example Journey"
SET @activityName = "Example Activity"
SET @errorCode = "123"
SET @source = "Email"
  
/* Log the error to the Data Extension */
InsertData(
    @errorLogDE,
    "Error_Message", @errorMessage,
    "Timestamp", FormatDate(Now(), "yyyy-MM-dd HH:mm:ss"),
    "Subscriber_Key", @subscriberKey,
    "Email_Address", @emailAddress,
    "Journey_Name", @journeyName,
    "Activity_Name", @activityName,
    "Error_Code", @errorCode,
    "Source", @source
)
  
/* Raise an error to stop the process */
RaiseError(@errorMessage, false)
]%%

Using Try Catch for Ampscript: 

AMPscript does not support try-catch blocks like traditional programming languages. However, you can achieve similar error-handling functionality by writing AMPscript within the SSJS try block as below:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<script runat="server">
    Platform.Load("Core", "1.1.1");
    try {
</script>

%%[ /* Your AMPscript block goes here */ 
SET @name = "John Doe"
SET @age =  Divide(10, 0) ]%%

<script runat="server">
    } catch (e) {
      // Capture the error object and output the error message
      Write(Stringify(e));
    }
</script>

Journey Builder: 

When configuring activities within a Journey, you can specify a "Failure" path. Connect this path to an "Update Contact" activity, which updates the error log Data Extension with the relevant error details. 

 Further Enhancement: 

  • Error severity classification can be added while capturing the error to the error log. EX: Critical, Major, Moderate, and Minor 
  • Based on the severity we can set up notification alerts. Ex: Triggered send for Critical issues and automation at specified intervals for other errors.

No comments:

Post a Comment

Powered by Blogger.