John
John

Reputation: 69

Netsuite SuiteScript not returning value of column

I'm having some trouble getting the value of a column in a saved search via SuiteScript. Below is my code:

function KW_AutoCloseOldRA() {
    var search = nlapiLoadSearch('transaction', 'customsearchopen_ras', null, null);
    var columns = search.getColumns();
    for (i = 0; i < columns.length; i++) {
        nlapiLogExecution('DEBUG', 'Column #' + i + ' is ' + columns[i].getName());
    }
    var results = search.runSearch();
    if (results) {
        results.forEachResult(getResults);
    }
}

function getResults(res) {
    var message = res.getValue('tranid');
    nlapiLogExecution('DEBUG', 'Result ' + message);
    return true;
}

The search produces two columns, and the name of those columns output as expected in the DEBUG entry (internalid is column 0 and tranid is column 1). When looping through the results however, res.getValue('tranid') is always null. I can't seem to find what I'm doing wrong here.

Upvotes: 0

Views: 1296

Answers (1)

Adolfo Garza
Adolfo Garza

Reputation: 3039

Try getting the value using the columns object and its index like this:

function KW_AutoCloseOldRA() {
    var search = nlapiLoadSearch('transaction', 'customsearchopen_ras', null, null);
    var columns = search.getColumns();
    for (i = 0; i < columns.length; i++) {
        nlapiLogExecution('DEBUG', 'Column #' + i + ' is ' + columns[i].getName());
    }
    var results = search.runSearch();
    if (results) {
        results.forEachResult(getResults);
    }
}

function getResults(res) {
    var cols =  res.getAllColumns();
    var message = res.getValue(cols[1]);
    nlapiLogExecution('DEBUG', 'Result ' + message);
    return true;
}

Upvotes: 3

Related Questions