[SalesForce] Marketing Cloud – Just trying to get record count of a Data Extension in SSJS

I followed part of the solution posted by @Gortonington here: How to improve this SSJS, but the following code does not reach the end of the DataExtension and unless I break, moreData always reads true and the script goes into an infinite loop until it aborts.

(BTW, I tried to do a query via ssjs to get the DE's record count and that failed when I added Count() to the SELECT. This is is my alternative approach)

My script

<script type="text/javascript" runat='server'>
    Platform.Load("Core", "1.1");

function Write(str) {
    Platform.Response.Write(str);
}
function Stringify(obj) {
    return Platform.Function.Stringify(obj);
}
try{
    var proxy = new Script.Util.WSProxy();
    var cols = ["account_id","audience_file"];
    var r = 0;
    var moreData = true;
    var reqID = null;
    var data;
    var recCount = 0;

do {
    moreData = false; //in case we don't make it in to the data conditional
    if (reqID) {props['ContinueRequest'] = reqID;} //set ContinueRequest ID if we have requestID from a previous run
    data = proxy.retrieve("DataExtensionObject[Audience_Transitory]", cols);

    if(data != null) {
        moreData = data.HasMoreRows;
        reqID = data.RequestID;
        if(data && data.Results) {
           Write("Has More: " + moreData);
           recCount = Number(data['Results'].length) + Number(recCount);
           Write("</br>recCount " + recCount);
        }
    }//if
    r++;

    if(r==40){break;}  //This is because moreData is always true.

} while(moreData);

Write("ttlCount: " + recCount + "</br>");

} catch (err) {
    //Platform.Function.InsertData("DEBUGGING",["DebugMessage","Timestamp","ImportType"],[err,d,"DebugScript"]);
}

</script>

Best Answer

The simpler solution to get the rowcount of a Data Extension is to utilize the DataExtensionRowcount() AMPscript function.

Simply put in %%[ Set @DECount = DataExtensionRowCount('myDE') ]%% and the return of @DECount will be the rowcount of your DE.

Related Topic