0

I am trying to move multiple old DEs into a new folder. And, with my limited experience with SFMC Objects & WSProxy/SSJS, I dug deep into multiple resources & samples codes like (Zuzanna - update DE) and @Gortonington(SFMC DE Inventory (Take 2)) and a sample code from Gortonington again (Retrieve DE by folder). Here is my code (many are sourced from these talented individuals), but I'm getting this error Message:

Line 22 CustomerKey Returned : [{"CustomerKey":"Einstein_MC_Predictive_Scores"}] {"message":"Error executing update call.","description":"ExactTarget.OMM.FunctionExecutionException: Error executing update call.\r\n Error Code: OMM_FUNC_CONTEXT_ERR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.ArgumentException: Object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' cannot be converted to type 'System.String'. - from mscorlib\r\n\r\n\r\n\r\n"}

<script runat=server>
    Platform.Load("Core","1");
    
    var prox = new Script.Util.WSProxy(); // Sets the Proxy - DO NOT CHANGE
    
    try { //Will attempt to run the below stuff
      var deArr = getAllDeInfo();  //returns all the DE info from this Business Unit 
      var custkey_Arr = []; //Araarso - move to new folder 
    
      for (var a=0; a<deArr.length;a++) { //Iterate through the array holding each of the objects from the DE info return
        var deData = deArr[a].Results;  //Set the Object for us to pull
    
        for (var e=0; e<deData.length; e++) { //iterate through the Results of that object, setting each value
          var name = deData[e].Name;
    
         //Call the AMPscript function to return DE rowcounts 
        var rowCount = Platform.Function.TreatAsContent('%'+'%=DataExtensionRowCount("' + name + '")=%' + '%');
        var rowCount_format = parseInt(rowCount,10);   //Araarso - format rowCount into Digit since it is string
    
        //Araarso - find current year 
        var dt = new Date();
        var date_end = addDate(dt, -12, 'months');
        
        //Araarso - variable for DE modified Date
        var mod_year = deData[e].ModifiedDate;
    
          if ( (rowCount_format > 1000000) && (mod_year < date_end)) {  //Araarso - condition to include only DE with 2K+ records - Modified/Created 12 Months ago.
            var deObj = {};
            var customerkey_v = deData[e].CustomerKey;
            deObj.CustomerKey = customerkey_v;
            custkey_Arr.push(deObj);
            Write('Line 22 CustomerKey Returned : ' + Stringify(custkey_Arr) + ' ');
     
            //Sets variables pulling in the meta data of the DEs
            var catID = deData[e].CategoryID
              
            //Call the AMPscript function to return DE rowcounts 
            var rowCount = Platform.Function.TreatAsContent('%'+'%=DataExtensionRowCount("' + name + '")=%' + '%');
    
            //Pulls the folder path of the data extension
            var list = [];
              list.push(name);
            var path = function(id) {
                if (id> 0) {
                    var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});
    
                    if(results) {
                      list.unshift(results[0].Name);
                      return path(results[0].ParentFolder.ID);
                    } 
                    else {
                      list = ['error in path retrieve']
                      return list;
                    }
                } 
                else {
                    return id;
                }
            };
            path(catID);
            folderPath = list.join("> ");
    
             if ( folderPath.indexOf("Dir") < 0 ) {   //Araarso -  Condition to EXCLUDE "DESelect folders"                                  
            
                //Araarso - move DE to new folder START
                var catID_v = 200;
    
                //Araarso - Move OLD DE to new folder END
                for(var i=0; i< custkey_Arr.length; i++) {
                    var res = prox.updateItem("DataExtension", {"CustomerKey":custkey_Arr[i], "CategoryID":catID_v});
                    Write(' Line 74 returns : ' + Stringify(res) + "<br>");
                    } 
           } // end restricting Dir folder (Line 69)
          }  //  end  Condition to include only DE with 2K+ records - Modified/Created 12 Months ago. (Line 36)
        }  //    end Results iterative (Line 14)
      }  //      end Array of DE Objects iterative (Line 11)
    
    } //end try (Line 8)
    
    catch(e) { // Instead of tossing an error or exception, this will display the error in an output
    
     Write(Stringify(e)) // Writes the error object to the page
    
    }
    
    function getAllDeInfo() {
      var cols = [
                    "ObjectID"
                    ,"CustomerKey"
                    ,"Name"
                    ,"CategoryID"
                    ,"CreatedDate"
                    ,"ModifiedDate"
                    ,"Client.ID"
                    ,"Description"
                    ,"CategoryID"
                ];
    
      var filter = {
              Property: "CategoryID"
              , SimpleOperator: "notEquals"
              , Value: 200 //do not look new folder
         };
     
    
    
      var reqID = null
      var moreData = true;
      var desc = [];
    
      while(moreData) {
        moreData = false;
        var data = reqID == null ?
        prox.retrieve("DataExtension", cols, filter):  
          prox.getNextBatch("DataExtension", reqID);
        if(data != null) {
          moreData = data.HasMoreRows;
          reqID = data.RequestID;
          if(data && data.Results) {
            desc.push(data);
          }
        }
      }
    
      return desc;
    }
    
    //Araarso - function to create date structure 
    function addDate(dt, amount, dateType) {
      switch (dateType) {
        case 'days':
          return dt.setDate(dt.getDate() + amount) && dt;
        case 'weeks':
          return dt.setDate(dt.getDate() + (7 * amount)) && dt;
        case 'months':
          return dt.setMonth(dt.getMonth() + amount) && dt;
        case 'years':
          return dt.setFullYear( dt.getFullYear() + amount) && dt;
      }
    }
    
    </script>
1
  • BTW - When I ran this code in the automation, it runs fine (with no errors) but no DE is moved to the new folder. However, when I ran it on the Cloud pages, I can see the error message May 11 at 18:56

1 Answer 1

0

Answered my own question - Finally figured out. Here is my code:

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

var prox = new Script.Util.WSProxy(); // Sets the Proxy - DO NOT CHANGE

try { //Will attempt to run the below stuff
  var deArr = getAllDeInfo();  //returns all the DE info from this Business Unit 
   
  for (var a=0; a<deArr.length;a++) { //Iterate through the array holding each of the objects from the DE info return
    var deData = deArr[a].Results;  //Set the Object for us to pull

    for (var e=0; e< deData.length; e++) { //iterate through the Results of that object, setting each value
         var name = deData[e].Name;
         var catID = deData[e].CategoryID;
         var CustomerKey_v = deData[e].CustomerKey
         Write(' Line 16 returns : ' + Stringify(CustomerKey_v) + "<br>");

     //Call the AMPscript function to return DE rowcounts 
    var rowCount = Platform.Function.TreatAsContent('%'+'%=DataExtensionRowCount("' + name + '")=%' + '%');
    var rowCount_format = parseInt(rowCount,10);   // format rowCount into Digit since it is string

    // find current year 
    var dt = new Date();
    var date_end = addDate(dt, -5, 'months');
  //var date_end = addDate(dt, -12, 'months');
    
    // variable for DE modified Date
    var mod_year = deData[e].ModifiedDate;

      if ( (rowCount_format > 2000) && (mod_year < date_end)) {  //Condition to include only DE with 2K+ records - Modified 12 Months ago.
        //Call the AMPscript function to return DE rowcounts 
        var rowCount = Platform.Function.TreatAsContent('%'+'%=DataExtensionRowCount("' + name + '")=%' + '%');

        //Pulls the folder path of the data extension
        var list = [];
          list.push(name);
        var path = function(id) {
            if (id> 0) {
                var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});

                if(results) {
                  list.unshift(results[0].Name);
                  return path(results[0].ParentFolder.ID);
                } 
                else {
                  list = ['error in path retrieve']
                  return list;
                }
            } 
            else {
                return id;
            }
        };
        path(catID);
        folderPath = list.join("> ");

                                     
         if ( folderPath.indexOf("Dir") < 0 ) { 
            //Folder to move DEs
            var catID_v = 0001;

            //execute 
             var res = prox.updateItem("DataExtension", {"CustomerKey":deData[e].CustomerKey, "CategoryID":catID_v});
             Write(' Line 64 returns : ' + Stringify(res) + "<br>");

       }  
      }  
    }  
  }  

} 

catch(e) { // Instead of tossing an error or exception, this will display the error in an output
           Write(Stringify(e)) // Writes the error object to the page
        }

function getAllDeInfo() {
  var cols = [
             "ObjectID"
                ,"CustomerKey"
                ,"Name"
                ,"CategoryID"
                ,"CreatedDate"
                ,"ModifiedDate"
                ,"Client.ID"
                ,"Description"
            ];

    var filter = {
                  Property: "CategoryID"
                  , SimpleOperator: "notEquals"
                  , Value:0001
                 } 


  var reqID = null
  var moreData = true;
  var desc = [];

  while(moreData) {
    moreData = false;
    var data = reqID == null ?
    prox.retrieve("DataExtension", cols, filter): prox.getNextBatch("DataExtension", reqID);
    if(data != null) {
      moreData = data.HasMoreRows;
      reqID = data.RequestID;
      if(data && data.Results) {
        desc.push(data);
      }
    }
  }

  return desc;
}

// function to create date structure 
function addDate(dt, amount, dateType) {
  switch (dateType) {
    case 'days':
      return dt.setDate(dt.getDate() + amount) && dt;
    case 'weeks':
      return dt.setDate(dt.getDate() + (7 * amount)) && dt;
    case 'months':
      return dt.setMonth(dt.getMonth() + amount) && dt;
    case 'years':
      return dt.setFullYear( dt.getFullYear() + amount) && dt;
  }
}

</script>

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.