Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Republish Triggered Send Definitions recursively by folder
<script runat="server">
Platform.Load("core","1");
var debug = false;
var logDE = DataExtension.Init("republish_trigger_log");
try {
/* CONFIG */
var parentCategoryID = 1264; // Triggered Sends
writeLog("All", "parentCategoryID", parentCategoryID);
if (debug) {
Write('<br>parentCategoryID: ' + parentCategoryID);
}
/* EXECUTION */
traverseFoldersForTSDs(parentCategoryID, folderPath);
/* FUNCTIONS */
function traverseFoldersForTSDs(categoryID) {
if (debug) {
Write('<br>categoryID: ' + categoryID);
}
var currentFolder = getCurrentFolder(categoryID);
var folderName = currentFolder[0].Name;
if (debug) {
Write('<br>folderName: ' + folderName);
}
// process TSDs in current folder
processFolderTSDs(categoryID)
// retrieve any child folders
var childFolders = getChildFolders(categoryID);
if (debug) {
Write('<br>childFolders: ' + Stringify(childFolders));
}
if (childFolders && childFolders.length > 0) {
// iterate through child folders and process for TSDs
for (var j = 0; j < childFolders.length; j++) {
var folder = childFolders[j];
var folderName = folder.Name;
var folderID = folder.ID;
if (debug) {
Write('<br>folderName: ' + folderName);
Write('<br>folderID: ' + folderID);
}
processFolderTSDs(folder.ID)
// recursively call this function with child folder and new parent
traverseFoldersForTSDs(childFolders[j].ID);
}
}
}
function getCurrentFolder(categoryID) {
var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:categoryID});
return results
}
function getChildFolders(categoryID) {
var results = Folder.Retrieve({Property:"ParentFolder.ID",SimpleOperator:"equals",Value:categoryID});
return results
}
function processFolderTSDs(categoryID) {
var prox = new Script.Util.WSProxy();
var obj = "TriggeredSendDefinition";
var tsdProps = ["CustomerKey", "TriggeredSendStatus","Name"];
var filter = {Property:"CategoryID", SimpleOperator:"equals", Value: categoryID};
var opts = { BatchSize: 1000 };
var reqID = null
var moreData = true;
var abort = false;
while (moreData && !abort) {
moreData = false;
reqProps = {};
var data = reqID == null ? prox.retrieve(obj, tsdProps, filter, opts, reqProps): prox.getNextBatch(obj, reqID);
if (data != null) {
moreData = data.HasMoreRows;
reqID = data.RequestID;
if (data && data.Results) {
if (debug) {
Write('<br>data.Results.length: ' + data.Results.length);
}
for (var i = 0; i < data.Results.length; i++) {
var CustomerKey = data.Results[i].CustomerKey;
var Name = data.Results[i].Name;
// Valid values are "New", "Inactive" (Paused), "Active" (Running), "Canceled" (Archived), "Deleted"
var TriggeredSendStatus = data.Results[i].TriggeredSendStatus;
if (debug) {
Write('<hr>CustomerKey: ' + CustomerKey);
Write('<br>Name: ' + Name);
Write('<br>TriggeredSendStatus: ' + TriggeredSendStatus);
}
if (TriggeredSendStatus == "Active") {
var tsd = TriggeredSend.Init(CustomerKey);
// PAUSE
var pause = tsd.Pause();
// PUBLISH
var updateObj = {};
updateObj.CustomerKey = CustomerKey;
updateObj.RefreshContent = true;
var publish = tsd.Update(updateObj);
// RESTART
var start = tsd.Start();
writeLog(CustomerKey, "republish", "pause: " + pause + ", publish: " + publish + ", start: " + start);
} else {
writeLog(CustomerKey, "skipped", "not active");
}
if (debug) {
//break;
}
}
}
}
}
}
} catch (e) {
if (debug) {
Write('<br>e: ' + Stringify(e));
}
}
function writeLog(customerKey, key, value) {
if (debug) {
Write('<br><b>' + key + '</b>: ' + value) ;
}
try {
logDE.Rows.Add({TriggeredSendCustomerKey: customerKey, Key: key, Value: value});
} catch (e) {
logDE.Rows.Add({TriggeredSendCustomerKey: customerKey, Key: "writeLog e", Value: Stringify(e)});
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment