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 ]%%-->
prox.setClientId({ "": mid }); //Impersonates the BU
inside two of the functions, need to change it back toprox.setClientId({ "ID": mid }); //Impersonates the BU
. That should take care of any ClientID issues.<!--
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.