verified
Count Data Extension recordsLearn how to count Data Extension records (rows) in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the Core and REST API methods.
Core
var result = TreatAsContent("\%\%=DataExtensionRowCount('" + de + "')=\%\%");
<script runat="server">
Platform.Load("core", "1");
try {
var de = "MyDataExtension";
var count = TreatAsContent("\%\%=DataExtensionRowCount('" + de + "')=\%\%");
var result = (count != null && count.length > 0) ? Number(count) : 0;
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
25
REST API
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "/data/v1/customobjectdata/key/" + customerKey + "/rowset?$pageSize=1";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
var result = content.count;
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "/data/v1/customobjectdata/key/" + customerKey + "/rowset?$pageSize=1";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
var result = content.count;
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
25
WARNING
This method uses an undocumented endpoint, which can potentially be removed in the future releases.
Reference
Ressources and references related to the current methods.
Official documentation