0

I have tried try catch block in my email template to get the error handled and then in catch block trying to do a triggered send activity to send email to the admin, but this is not working.

Is there any way to achieve this, if the triggered send is queued the admin should get an alert message with the particular error.

(Adding the code I have been trying but getting an error as maxQueueArray is undefined)

<script runat=server>
  Platform.Load("Core","1.1.1");

  var mid = 'hardcodedMID';
  var dev = 0;
  var allTriggers = 1;

  var alertEmailAddress = 'me@company.com';// adding the admin email id
  var alertSubKey = 'me@company.com'; //adding the admin email id
  var now = new Date();
  var start = now.getTime();
  var timeOut = 1680000; //28 minutes
  //60000 milliseconds in a minute

  if (allTriggers) {
    var tsdArray = getTSDKeys(mid);
    var length = tsdArray.Results.length;
  } else {
    var tsdArray = ["ExternalkeyofTSD1","ExternalkeyofTSD2","ExternalkeyofTSD3","ExternalkeyofTSD4"]
    var length = tsdArray.length;
    var maxQueueArray = [1,1,1,1,1]; //Enter max queue here. Can make an array as well, if different maxes per TSD
  }

  if (!maxQueueArray || maxQueueArray.length == 0) {
    var maxQueueDefault = 1; //Default for if not using maxQueueArray
  }

  if (dev) {
    timeOut = 10000;
    maxQueueArray = [];
    maxQueueDefault = 1;
  }

  var alertArray = []
  var queueAlertArray = []

  do {

    var failures = 0;

    for (i=0; i < length; i++) {
      if(allTriggers) {
        var customerKey = tsdArray.Results[i].CustomerKey
      } else {
        var customerKey = tsdArray[i]
      }
      var queued = getTSDQueue(customerKey);
      var alreadyFail = Stringify(alertArray).indexOf(customerKey);
      dev ? Write('<hr><br>') : '';
      dev ? Write('CustomerKey: ' + customerKey + '<br>') : '';
      dev ? Write('Queue: ' + queued + '<br>') : '';
      dev ? Write('RunTime: ' + (new Date().getTime() - start) + '<br>') : '';

      var queueArrLength = maxQueueArray.length;

      // changes maxQueue to array value if exist and equal to i
      if (maxQueueArray.length > 0 && maxQueueArray.length <= i) {
        var maxQueue = maxQueueArray[i];
      } else {
        var maxQueue = maxQueueDefault;
      }

      if (queued > maxQueue) {

        if (alreadyFail < 1) {
          dev ? Write('<span style="color:red; font-weight:bold;">Queue Failure</span><br>') : '';
          alertArray.push(customerKey)
          queueAlertArray.push(queued)
          failures += 1
        }
      }
    }

    dev ? Write('<br>FAILURES: ' + failures + '<br><br><br>') : '';

    if (failures > 0) {
      var alertStr = Stringify(alertArray).replace('[','').replace(']','');
      var queueAlertStr = Stringify(queueAlertArray).replace('[','').replace(']','');
      var alertTrigger = sendTSDAlert(alertStr,queueAlertStr,alertEmailAddress,alertSubKey)
      dev ? Write('<br>' + Stringify(alertTrigger) + '<br>') : '';
    }

  } while((new Date().getTime() - start) < timeOut)



  /************************* FUNCTION LIST ************************/

  function getTSDKeys(mid) {
    var prox = new Script.Util.WSProxy();

    /* Set ClientID */
    if (mid) {
      prox.setClientId({ "": mid }); //Impersonates the BU
    }

    var cols = ["CustomerKey", "TriggeredSendStatus"];
    var res = prox.retrieve("TriggeredSendDefinition", cols, filter);

    var results = res

    return results;
  }

  function getTSDQueue(customerKey) {

    var prox = new Script.Util.WSProxy();

    /* Set ClientID */
    if (mid) {
      prox.setClientId({ "": mid }); //Impersonates the BU
    }


    var cols = ["CustomerKey","Queued"];
    var filter = {
        Property: "CustomerKey",
        SimpleOperator: "Equals",
        Value: customerKey
    };
    var res = prox.retrieve("TriggeredSendSummary", cols, filter);

    var queue = res.Results[0].Queued

    return queue;
  }

  function sendTSDAlert(alertStr,queueAlertStr,emailAddress,subKey) {

    var proxy = new Script.Util.WSProxy();
    var customerKey = "TSD_AlertTrigger"; // adding the triggered send defination external key
    var name = "TSD_AlertTrigger"; // adding the triggered send defination name

    var ts= {
           TriggeredSendDefinition: {CustomerKey: customerKey, Name: name}
           ,Subscribers: [ {
                  EmailAddress: emailAddress
                , SubscriberKey: subKey
                , Attributes: [
                    {
                     Name: 'tsdList',
                      Value: alertStr
                   },
                   {
                    Name: 'tsdQueue',
                    Value: queueAlertStr
                   }
                ]
            }]

        };

    var res = proxy.createItem("TriggeredSend", ts);

    return res;
  }

  </script>

Below is the email template code I am using:

    <!-- %%[ 
    set @tsdList = AttributeValue("tsdList")
    set @tsdQueue = AttributeValue("tsdQueue")

    set @tsdListRows = BuildRowsetFromString(@tsdList,",")
    set @tsdQueueRows = BuildRowsetFromString(@tsdQueue,",")

    ]%%-->
    The following Triggers have exceed queue limits:<br>
    <!-- %%[FOR @i=1 TO Rowcount(@tsdListRows) DO
     SET @RowList = Row(@tsdListRows,@i)
     SET @RowList1 = Field(@RowList,"RowList1")

     SET @RowQueue = Row(@tsdQueueRows,@i)
     if @i >= 1 THEN
     ]%%-->
       <hr>
     <!--  %%[ endif ]%%-->
     <br>
     Trigger CustomerKey: <!-- %%=Substring(TRIM(Field(@RowList,1)),2,SUBTRACT(Length(TRIM(Field(@RowList1,1))),2))=%%--><br>
     Trigger Queue Count: <!-- %%=Field(@RowQueue,1)=%%--><br>
    <br>
    <!--%%[ NEXT @i ]%%-->
  • You tsdArray and maxQueueArray must have same length – Lukas Lunow May 6 at 16:59
  • @LukasLunow , Oh yes, I have mentioned that correctly, actually while posting here I missed to mention. The error what I am getting is maxQueueArray is not defined, as I am using this for same BU I have kept the client id's empty, I am very new to ssjs and unable to get the exact issue in this scenario. – Krisha May 6 at 17:11
  • Inside of the functions. You changed it to prox.setClientId({ "": mid }); //Impersonates the BU inside two of the functions, need to change it back to prox.setClientId({ "ID": mid }); //Impersonates the BU. That should take care of any ClientID issues. – Gortonington May 6 at 18:05
  • After that, I was able to run it successfully using your code. Both for 'All TSDs' and for a set array of them. – Gortonington May 6 at 18:06
  • @Krisha You are commenting out the outputs. Your inline AMPscript should not have <!-- and --> around them. Remove those and it should display as expected. The blocks are fine as long as you are not outputting content inside them. But if you put comments around the inline functions (%%= =%%) it will hide them. – Gortonington May 7 at 15:53
2

The issue with queued emails in triggered sends is that they at this point don't throw an explicit error which you can query in the system. The error is placed in the log only the moment the send status is set to errored. What I would advise you to do, first of all, is to create monitoring of your triggered sends, to check for the queue length. Gortonington has created SSJS for this particular purpose, which is quite straightforward to implement. You can get the complete code from here: https://gortonington.com/alert-for-your-triggered-send-queue-limits-in-sfmc/

| improve this answer | |
  • Thanks @lukas, I am trying this but was unable to get it published in cloudpages, when I added a try catch I am getting this error: Error while setting clientID, the client id is correct which I have used in the code(I got it from the installed packages with all the access) – Krisha May 6 at 12:55
  • @Krisha - Once again, I think this is because SFMC has named multiple things multiple names and some names are duplicated on unrelated things - leading to confusion. The clientID in this case was to reference the MID of the BU, not the clientID in your package for API. You can also leave this empty to utilize the current BU. – Gortonington May 6 at 13:22
  • @lukas, thanks for the comment, you mean I just have to mention the MID, I did the same and now there is some issue with the maxqueuearray, error: "maxQueueArray" is not defined, but it defined. – Krisha May 6 at 14:02
  • @Gortonington can you help me with this? – Krisha May 6 at 14:15
  • Can you post what you have (removing PII, etc of course)? likely its a syntax error somewhere that is tripping it up – Gortonington May 6 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.