verified
Retrieve all records from a Data ExtensionLearn how to retrieve all Data Extension records (rows) in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the WSProxy method.
WSProxy
var api = new Script.Util.WSProxy();
var config = {
name: "MyDataExtension",
cols: [
"SubscriberKey",
"FirstName",
"LastName",
"EmailAddress"
],
filter: null,
opts: {
BatchSize: 300
},
props: {
QueryAllAccounts: false
}
}
var result = [],
moreData = true,
reqID = req = null;
while(moreData) {
moreData = false;
if(reqID) config.props.ContinueRequest = reqID;
var req = api.retrieve("DataExtensionObject[" + config.name + "]", config.cols, config.filter, config.opts, config.props);
if (req) {
moreData = req.HasMoreRows;
reqID = req.RequestID;
var results = req.Results;
for (var k in results) {
var props = results[k].Properties;
var o = {};
for (var i in props) {
var key = props[i].Name;
var val = props[i].Value
if (key.indexOf("_") != 0) o[key] = val;
}
result.push(o);
}
}
}
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var config = {
name: "MyDataExtension",
cols: [
"SubscriberKey",
"FirstName",
"LastName",
"EmailAddress"
],
filter: null,
opts: {
BatchSize: 300
},
props: {
QueryAllAccounts: false
}
}
var result = [],
moreData = true,
reqID = req = null;
while(moreData) {
moreData = false;
if(reqID) config.props.ContinueRequest = reqID;
var req = api.retrieve("DataExtensionObject[" + config.name + "]", config.cols, config.filter, config.opts, config.props);
if (req) {
moreData = req.HasMoreRows;
reqID = req.RequestID;
var results = req.Results;
for (var k in results) {
var props = results[k].Properties;
var o = {};
for (var i in props) {
var key = props[i].Name;
var val = props[i].Value
if (key.indexOf("_") != 0) o[key] = val;
}
result.push(o);
}
}
}
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
[
{
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"FirstName": "John",
"LastName": "Doe",
"EmailAddress": "example@mail.com"
},
{
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"FirstName": "John",
"LastName": "Doe",
"EmailAddress": "john.doe@anywmail.com"
},
{
"SubscriberKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"FirstName": "Jane",
"LastName": "Doe",
"EmailAddress": "jane.doe@othermail.com"
}
]
Reference
Ressources and references related to the current methods.
Data Extensions Async API
SOAP object