0

At the moment our emails are using a try and catch SSJS wrapping and stringify to log the error into a DE. We need to be able to catch any error happening due to the use of different blocks. At the moment we use SSJS try and catch, the following error has been logged into the debug DE however it has also been emailed to the customer .

Call to update the salesforceobject Account ID = 0017Y00001XXXXXXX failed! Error status code: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY\nError message: AccountTrigger: execution of AfterUpdate\n\ncaused by: System.DmlException: Update failed. First exception on row 0 with id 0PK7YXXXXX015YtXXXX; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []\n\nClass.IndividualService.syncIndividualFields: line 77, column 1\nClass.AccountTriggerHandler.handleAfterUpdate: line 139, column 1\nTrigger.AccountTrigger: line 37, column 1"

We don't want the customer to receive the email with the error message. And we need to block the send when this happens.

Support suggested to use Raise Error to block the send completely as SSJS will log but still send. I only find example with IF statement, but I don't have a way to see if the record is locked. So I thought to use the SSJS to catch the error and then raise the error with ampscript.

I tried to add the raise error in the SSJS catch part. The email is not sent but also the error is not logged into the data extension. I am not sure if the following syntax incorrect? Should the raise error be outside of the SSJS?

As I cannot reproduce the record lock issue I tried by adding the wrong contact key however seems like the try and catch is not catching this error and not logging. So I don't understand if it's a limitation or a syntax issue

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
          
      <head>
      </head>
      <body>
        
        **<script runat="server">
          Platform.Load("Core","1");
          try{
        </script>**

<div style="font-size:0; line-height:0;">
         
              ***..... email blocks and content......***
                                           
                                      
                                    
        <script runat="server">
          }
          catch(e) {
            Write(Stringify(e.message))
            Platform.Function.InsertDE("EmailDebugLog" ,["Template","ErrorMessage"],["1",Stringify(e.message)]);
            RaiseError("Error occurred",true)
          }
        </script>
    
      </body>
    </html>
2
  • 2
    Can you update your question to include details about why you need try/catch in your email? Commented Oct 15, 2021 at 12:02
  • @AdamSpriggs does this help?
    – SYUYDY
    Commented Oct 18, 2021 at 7:15

2 Answers 2

0

Probably not what you want to hear, but you should not rely on a real-time call to a SF object to retrieve a coupon in your email scripting.

If you want a performant and stable process, I'd recommend storing your coupons in a Data Extension and doing a simple lookup or claimrow to retrieve them at send time.

You'll forever be battling locking and latency issues if you continue down this path.

Since your account is integrated, why not just refresh a coupon DE of all SF Contacts populated with an hourly SQL query?

4
  • We are storing the coupons in a DE and assigning them via the email. The problem is that after we assign them, the view online cannot tell what coupon was assigned. I am upserting now the DE with jobid+batchId and subscriber key to see if we can force the VAWP will retrieve the correct coupon.As at the moment it tries to retrieve the coupon assigned in SF but someone can be assigned more than 1.
    – SYUYDY
    Commented Oct 18, 2021 at 13:55
  • Also I just realised this is the other question open, the reason why we have the raise error andtry to solve here is because we are trying to pass a cloud page URL in CRM but the record at times is locked by CRM running activities and the try and a catch sends the error to the customer.I am trying to stop the error from being sent if it happens and to just be logged. But try and catch only catches is and sends the error. I now added the raise error and it's not sending but it's not logging the error
    – SYUYDY
    Commented Oct 18, 2021 at 13:57
  • Please update your question to include the claimrow code. Commented Oct 18, 2021 at 16:32
  • I updated my question as I think the coupon part is confusing as it's nothing to do with the issue
    – SYUYDY
    Commented Oct 19, 2021 at 9:30
0

I managed to make the code working, moving the raise error outside the try and catch script. The code below captures error with try and catch and logs it into a DE (EmailDebugLog) if an error occurs raise error ampscript function blocks the send.

To test try and catch you can use the throw function to force an error.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
      <!--
%%[
    var @errorred
    set @errorred = false
    var @template
    set @template = AttributeValue('emailname_')
    var @job
    set @job = AttributeValue('jobid')
]%%


  <head>
   content
  </head>
  <body>
Content    
<script runat="server">
      }
      catch(e) {
        Write(Stringify(e.message))
       Platform.Function.InsertDE("EmailDebugLog",["Template","ErrorMessage","JobID"], [Variable.GetValue("@template"), Stringify(e.message), Variable.GetValue("@job")])

        Variable.SetValue("@errorred", true)
      }
    </script>
%%[
  IF (@errorred) THEN
    RaiseError("Error occurred",true,1)
  ENDIF
]%%
  </body>
</html>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .