1

under Email Studio -> Content Menu item -> My Emails folder, we keep our sent emails in various subfolders (say A, B, C, D). We'd like an automated method of moving emails older than 7 days in each folder to archive folders being kept under the same "My Emails" directory (say archive-A, archive-B, etc), something that would run every day.

Does anyone know of a good way of doing this or know of a code sample of something similar already being done? My first preference would be Automation Studio directly but there's no "move emails between subfolders" option that I can see. Second preference would be Server Side Java Script that I would run daily via Automation Studio. [1] is not conclusive, but appears I may be able to retrieve emails based on child folder name and their age, and update each email's parent folder to that of the archive folder before resaving the email to cause the move. Finally I can try working the Marketing Cloud API's Email and DataFolder objects using SOAP calls, however the "folder" property for an email that I would want to modify is listed as deprecated and read-only[2] with no apparent alternative given to replace it.

Any suggestions welcome.

[1] http://help.marketingcloud.com/nl/documentation/exacttarget/content/server_side_javascript/server_side_javascript_syntax_guide/core_library_server_side_javascript_functions/email_server_side_javascript_functions/

[2] https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/email.htm

1 Answer 1

1

OK, was able to figure it out using SSJS in an Email Studio script (Email Studio -> Interactions -> Scripts) and Automation Studio to run the SSJS each night. For debugging, I ran the script within CloudPages (made a test HTML page), allowing me to see the "Write" results I have in the script below (note the output won't appear while running via Email Studio -> Interactions -> Scripts or via Automation Studio, but no harm keeping the Write statements there.)

<script runat="server">
Platform.Load("core","1");

// date queries in Salesforce apparently use string comparisons 
function formatDate(dateObject) {
  // required format: 'YYYY-MM-DDTxx:xx:xx.xxx'
  return dateObject.getFullYear() + "-" + 
         (dateObject.getMonth() < 9 ? "0" : "") +
         (dateObject.getMonth() + 1) + "-" + 
         (dateObject.getDate() < 10 ? "0" : "") +
         dateObject.getDate() + 'T00:00:00.000';
}

try {
  var folderArray = [];
  // add one element for each folder that needs archiving (moving email from latest to archive folder)
  folderArray.push({ "latest": “folder1”, "archive": “folder1-archive” });
  folderArray.push({ "latest": “folder2”, "archive": “folder2-archive” });

  // move emails older than one week ago
  var dateCutoff = new Date();
  dateCutoff.setDate(dateCutoff.getDate() - 7);
  var dateCutoffStr = formatDate(dateCutoff);

  for (var fIdx = 0, len = folderArray.length; fIdx < len; fIdx++) {        
     var fileFilter = {Property : "CategoryID", SimpleOperator : "equals", Value : folderArray[fIdx].latest};
     var dateFilter = {Property : 'CreatedDate', SimpleOperator : 'lessThan', Value : dateCutoffStr};
     var combinedFilter = {LeftOperand : fileFilter, LogicalOperator : 'AND', RightOperand : dateFilter};

     var results = Email.Retrieve(combinedFilter);

     for (var i=0; i < results.length ; ++i) {
         // results retrieved must be put into email object to be updated
         // see: https://preview.tinyurl.com/ha7b6qa
         // an email is identified by its CustomerKey.
         // see: https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/email.htm
         var emailObj = Email.Init(results[i].CustomerKey);
         // to move an email, update its CategoryID to that of the new folder.
         emailObj.Update({ "CategoryID" : folderArray[fIdx].archive });
     }

     Write("latest: " + folderArray[fIdx].latest + " archive: " + folderArray[fIdx].archive + " emails moved: " + results.length);  
  }        
} catch (ex) {
  Write("Exception: " + Stringify(ex));
}  

2
  • you are a visionary. This is exactly what I was looking for... but one problem, I don't have a "Scripts" folder in Interactions. Is that something that has to be turned on by Salesforce? I am skilled with JavaScript, but have never used the Server Side JS that Marketing Cloud offers. Commented Jul 11, 2017 at 14:01
  • 1
    Yes, you need to contact global support or your rep to have this implemented (for some reason it is not implemented by default). It should be no cost as long as you already have Automation Studio. Commented Jul 12, 2017 at 12:50

You must log in to answer this question.

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