如何从 SuiteScript 保存的搜索中获取超过 1000 条记录?

How to get over 1000 records from a SuiteScript Saved Search?

下面是我使用 SuiteScript 运行 在 NetSuite 中保存搜索的代码,使用保存的搜索结果创建 CSV,然后通过电子邮件发送 CSV。问题是,结果限制为 1000 条记录。我已经研究过这个问题,看来解决方案是 运行 一个以 1000 为增量进行切片的循环。我认为用于切片搜索的示例也在下面。

但是,我似乎无法将切片合并到我的代码中。谁能帮我把切片代码和我原来的搜索代码结合起来?

var search = nlapiSearchRecord('item', 'customsearch219729'); 

// Creating some array's that will be populated from the saved search results 
var content = new Array(); 
var cells = new Array(); 
var temp = new Array(); 
var x = 0; 

// Looping through the search Results 
for (var i = 0; i < search.length; i++) { 
var resultSet = search[i]; 
// Returns an array of column internal Ids
var columns = resultSet.getAllColumns(); 

// Looping through each column and assign it to the temp array 
for (var y = 0; y <= columns.length; y++) { 
temp[y] = resultSet.getValue(columns[y]); 
} 
// Taking the content of the temp array and assigning it to the Content Array. 
content[x] += temp; 
// Incrementing the index of the content array 
x++; 
} 

//Inserting headers
content.splice(0, 0, "sku,qty,");

// Creating a string variable that will be used as the CSV Content 
var contents; 

// Looping through the content array and assigning it to the contents string variable. 
for (var z = 0; z < content.length; z++) { 
contents += content[z].replace('undefined', '') + '\n';
}
// Creating a csv file and passing the contents string variable. 
var file = nlapiCreateFile('InventoryUpdate.csv', 'CSV', contents.replace('undefined', ''));

// Emailing the script.
function SendSSEmail()
{
   nlapiSendEmail(768, 5, 'Inventory Update', 'Sending saved search via scheduled script', 'cc@email.com', null, null, file, true, null, 'cc@email.com');
}

下面的代码是我发现的用于return超过1000条记录的示例。同样,作为新手,我似乎无法将切片合并到我原来的功能 SuiteScript 中。当然非常感谢任何帮助。

var filters = [...];
var columns = [...];
var results = [];
var savedsearch = nlapiCreateSearch( 'customrecord_mybigfatlist', filters, columns );
var resultset = savedsearch.runSearch();
var searchid = 0;
do {
    var resultslice = resultset.getResults( searchid, searchid+1000 );
    for (var rs in resultslice) {
        results.push( resultslice[rs] );
        searchid++;
    }
} while (resultslice.length >= 1000);
return results;

下一行:

var savedsearch = nlapiCreateSearch( 'customrecord_mybigfatlist', filters, columns );

可以像这样适应您自己保存的搜索:

var savedsearch = nlapiLoadSearch('item', 'customsearch219729');

希望对您有所帮助。

试试这个:

function returnCSVFile(){

    function escapeCSV(val){
        if(!val) return '';
        if(!(/[",\s]/).test(val)) return val;
        val = val.replace(/"/g, '""');
        return '"'+ val + '"';
    }


    function makeHeader(firstLine){
        var cols = firstLine.getAllColumns();
        var hdr = [];
        cols.forEach(function(c){
            var lbl = c.getLabel(); // column must have a custom label to be included.
            if(lbl){
                hdr.push(escapeCSV(lbl));
            }
        });
        return hdr.join(",");
    }

    function makeLine(srchRow){
        var cols = srchRow.getAllColumns();
        var line = [];
        cols.forEach(function(c){
            if(c.getLabel()){
                line.push(escapeCSV(srchRow.getText(c) || srchRow.getValue(c)));
            }
        });
        return line.join(",");
    }

    function getDLFileName(prefix){
        function pad(v){ if(v >= 10) return v; return "0"+v;}
        var now = new Date();
        return prefix + '-'+    now.getFullYear() + pad(now.getMonth()+1)+ pad(now.getDate()) + pad( now.getHours())    +pad(now.getMinutes()) + ".csv";
    }


    var srchRows = getItems('item', 'customsearch219729'); //function that returns your saved search results

    if(!srchRows)   throw nlapiCreateError("SRCH_RESULT", "No results from search");


    var fileLines = [makeHeader(srchRows[0])];

    srchRows.forEach(function(soLine){
        fileLines.push(makeLine(soLine));
    });



var file = nlapiCreateFile('InventoryUpdate.csv', 'CSV', fileLines.join('\r\n'));
nlapiSendEmail(768, 5, 'Test csv Mail','csv', null, null, null, file);
}

function getItems(recordType, searchId) {
    var savedSearch = nlapiLoadSearch(recordType, searchId);
    var resultset = savedSearch.runSearch();
    var returnSearchResults = [];
    var searchid = 0;
    do {
        var resultslice = resultset.getResults(searchid, searchid + 1000);
        for ( var rs in resultslice) {
            returnSearchResults.push(resultslice[rs]);
            searchid++;
        }
    } while (resultslice.length >= 1000);

    return returnSearchResults;
}

我查看了您的代码,但您似乎在生成的 CSV 文件中缺少标签 headers。如果您必须使用现有代码,则只需替换

var search = nlapiSearchRecord('item', 'customsearch219729'); 

var search = getItems('item', 'customsearch219729');

并且只需使用提到的辅助函数即可摆脱 1000 个结果限制。

干杯!

很高兴自发布和回复以来已经有一段时间了,但对于其他寻求对原始问题的更通用响应的人来说,以下代码应该足够了:

var search = nlapiLoadSearch('record_type', 'savedsearch_id');
var searchresults = search.runSearch();
var resultIndex = 0;
var resultStep = 1000;
var resultSet;
do {
    resultSet = searchresults.getResults(resultIndex, resultIndex + resultStep);    // retrieves all possible results up to the 1000 max  returned
    resultIndex = resultIndex + resultStep;                     // increment the starting point for the next batch of records
    for(var i = 0; !!resultSet && i < resultSet.length; i++){   // loop through the search results
       // Your code goes here to work on a the current resultSet (upto 1000 records per pass)
    }
} while (resultSet.length > 0)

另外值得一提的是,如果您的代码要更新字段/记录/创建记录,您需要牢记脚本治理。 将您的代码移动到预定脚本以处理大量记录会更高效,并允许您处理治理。