Created
April 13, 2023 09:15
-
-
Save leto96/c686a8e58e2d0177784ce15b80f4e79f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function DataExtensionRecordsOperator(configuration){ | |
var api; | |
var customerKey; | |
if(!configuration) Platform.Function.RaiseError('An configuration Object is required'); | |
if(!configuration.api) Platform.Function.RaiseError('Api attribute is required'); | |
if(!configuration.dataExtensionCustomerKey && !configuration.dataExtensionName) Platform.Function.RaiseError('dataExtensionCustomerKey or dataExtensionName attribute is required'); | |
if(configuration.dataExtensionCustomerKey && typeof configuration.dataExtensionCustomerKey != 'string') Platform.Function.RaiseError('dataExtensionCustomerKey must be a String'); | |
api = configuration.api; | |
if(configuration.dataExtensionCustomerKey == null || configuration.dataExtensionCustomerKey == ''){ | |
var simpleFilter = { | |
Property: 'Name', | |
SimpleOperator: 'equals', | |
Value: configuration.dataExtensionName | |
} | |
customerKey = api.retrieve("DataExtension", ["CustomerKey"], simpleFilter).Results[0].CustomerKey; | |
}else{ | |
customerKey = configuration.dataExtensionCustomerKey; | |
} | |
function getRecords(options){ | |
// options Object can have filter or headers | |
if(!!options && typeof options != 'object') Platform.Function.RaiseError('Options must be an object or omitted'); | |
var headers; | |
var filter; | |
if(!!options && options.headers){ | |
headers = options.headers; | |
}else{ | |
headers = retrieveFieldNames(customerKey, api); | |
} | |
if(!!options && options.filter) filter = options.filter; | |
var config = { | |
customerKey: customerKey, | |
cols: headers | |
} | |
var records = [], | |
moreData = true, | |
reqID = data = null; | |
while (moreData) { | |
moreData = false; | |
if (reqID == null) { | |
if(filter == undefined || filter == null){ | |
data = api.retrieve("DataExtensionObject[" + config.customerKey + "]", config.cols); | |
}else{ | |
data = api.retrieve("DataExtensionObject[" + config.customerKey + "]", config.cols, filter); | |
} | |
if(data.Status.substring(0, 5) == 'Error'){ | |
Platform.Function.RaiseError('Something went wrong: ' + data.Status); | |
} | |
} else { | |
data = api.getNextBatch("DataExtensionObject[" + config.customerKey + "]", reqID); | |
} | |
if (data != null) { | |
moreData = data.HasMoreRows; | |
reqID = data.RequestID; | |
for (var i = 0; i < data.Results.length; i++) { | |
var result_list = data.Results[i].Properties; | |
var obj = {}; | |
for (k in result_list) { | |
var key = result_list[k].Name; | |
var val = result_list[k].Value | |
if (key.indexOf("_") != 0) obj[key] = val; | |
} | |
records.push(obj); | |
} | |
} | |
} | |
return records; | |
} | |
function pipeExecuteInAllRecords(options, cb){ | |
// options Object can have filter or headers | |
if(!!options && typeof options != 'object') Platform.Function.RaiseError('Options must be an object or omitted'); | |
var filter; | |
headers = retrieveFieldNames(customerKey, api); | |
if(!!options && options.filter) filter = options.filter; | |
var config = { | |
customerKey: customerKey, | |
cols: headers | |
} | |
var records = [], | |
moreData = true, | |
reqID = data = null; | |
while (moreData) { | |
moreData = false; | |
if (reqID == null) { | |
if(filter == undefined || filter == null){ | |
data = api.retrieve("DataExtensionObject[" + config.customerKey + "]", config.cols); | |
}else{ | |
data = api.retrieve("DataExtensionObject[" + config.customerKey + "]", config.cols, filter); | |
} | |
if(data.Status.substring(0, 5) == 'Error'){ | |
Platform.Function.RaiseError('Something went wrong: ' + data.Status); | |
} | |
} else { | |
data = api.getNextBatch("DataExtensionObject[" + config.customerKey + "]", reqID); | |
} | |
if (data != null) { | |
moreData = data.HasMoreRows; | |
reqID = data.RequestID; | |
for (var i = 0; i < data.Results.length; i++) { | |
var result_list = data.Results[i].Properties; | |
var obj = {}; | |
for (k in result_list) { | |
var key = result_list[k].Name; | |
var val = result_list[k].Value | |
if (key.indexOf("_") != 0) obj[key] = val; | |
} | |
records.push(obj); | |
} | |
cb(records); | |
records = []; // clear records, free up memory | |
} | |
} | |
} | |
function retrieveFieldNames(customerKey, api) { | |
var filter = { | |
Property: "DataExtension.CustomerKey", | |
SimpleOperator: "equals", | |
Value: customerKey | |
}; | |
var req = api.retrieve("DataExtensionField", ["Name"], filter); | |
var fields = req.Results; | |
var out = []; | |
for (k in fields) { | |
out = out.concat(fields[k].Name); | |
} | |
return out; | |
} | |
return { | |
getRecords: getRecords, | |
pipeExecuteInAllRecords: pipeExecuteInAllRecords | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment