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:
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>