0

I am trying retrieve all Automations from a Business Unit and insert them in a Data Extension ussing SSJS. It works fine when I retrieve them in a Cloud Page but when I try to insert them into a Data Extension I am getting the following error: "There was an error Adding the row(s)." I am not sure if there's something wrong with the code or with the Data Extension.

This is the Data Extension:

enter image description here

This is the code I am using:

<script runat="server">
Platform.Load("Core", "1");
var api = new Script.Util.WSProxy();

// External key of the target Data Extension
var DE_EXTERNAL_KEY = "external-key";

try {
  var automations = retrieveAllAutomations();
  // Insert the retrieved data into the target Data Extension
  var response = insertDataIntoDE(automations);
  Write(Stringify(response));
} catch (err) {
  Write(Stringify(err));
}

function retrieveAllAutomations() {
  var out = [],
    moreData = true,
    reqID = data = null;

  var cols = [
    "Name",
    "Description",
    "CustomerKey",
    "IsActive",
    "CreatedDate",
    "ModifiedDate",
    "Status",
    "CategoryID",
    "LastRunTime",
    "ScheduledTime",
    "LastSaveDate",
    "ModifiedBy",
    "LastSavedBy",
    "CreatedBy",
    "AutomationType",
    "RecurrenceID"
  ];

  var filter = {
    Property: 'Status',
    SimpleOperator: 'IN',
    Value: [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
  };

  while (moreData) {
    moreData = false;

    if (reqID == null) {
      data = api.retrieve("Automation", cols, filter);
    } else {
      data = api.getNextBatch("Automation", reqID);
    }

    if (data != null) {
      moreData = data.HasMoreRows;
      reqID = data.RequestID;
      for (var i = 0; i < data.Results.length; i++) {
        out.push(data.Results[i]);
      }
    }
  }

  return out;
}

// Insert the retrieved data into the target Data Extension
function insertDataIntoDE(data) {
  var objs = [];

  for (var i = 0; i < data.length; i++) {
    var obj = {
      "Name": data[i].Name,
      "Description": data[i].Description,
      "CustomerKey": data[i].CustomerKey,
      "IsActive": data[i].IsActive,
      "CreatedDate": data[i].CreatedDate,
      "ModifiedDate": data[i].ModifiedDate,
      "Status": data[i].Status,
      "CategoryID": data[i].CategoryID,
      "LastRunTime": data[i].LastRunTime,
      "ScheduledTime": data[i].ScheduledTime,
      "LastSaveDate": data[i].LastSaveDate,
      "ModifiedBy": data[i].ModifiedBy,
      "LastSavedBy": data[i].LastSavedBy,
      "CreatedBy": data[i].CreatedBy,
      "AutomationType": data[i].AutomationType,
      "RecurrenceID": data[i].RecurrenceID
    };

    objs.push(obj);
  }

  var de = DataExtension.Init(DE_EXTERNAL_KEY);
  var result = de.Rows.Add(objs);
  Write("Result: " + result.length + " rows added");
  return result;
}
</script>
5
  • For me it's almost always mismatched type/length of fields.
    – Ted Chan
    May 8, 2023 at 0:08
  • yeah, name, customerkey, description and recurranceID all seem a bit low on max length - potentially others as well. Likely as @TedChan said, its a mismatch on length May 8, 2023 at 14:30
  • Thanks for replying. I have tried editing the type/length of the fields but I'm still getting the same error :/
    – pmleal07
    May 8, 2023 at 18:20
  • Could it be the format of the dates? I haven't dealt with dates for a while so not sure about that. I would try to remove some of the fields in the object and see if they can be inserted correctly.
    – Ted Chan
    May 8, 2023 at 23:55
  • Thanks for your reply. I used Platform.Function.InsertData instead of DataExtension.Init and it worked.
    – pmleal07
    May 16, 2023 at 14:55

1 Answer 1

0

I changed the way of inserting the data to the extension. I switched DataExtension.Init to Platform.Function.InsertData and it worked.

You must log in to answer this question.

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