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

时间:2015-06-26 11:35:23

标签: netsuite

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

但是,我似乎无法将切片合并到我的代码中。任何人都可以帮我把切片代码和原始搜索代码结合起来吗?

db.rawQuery("DELETE FROM " + table + " WHERE " + args);

以下代码是我发现的用于返回超过1000条记录的示例。同样,作为一个新手,我似乎无法将切片结合到我原来的功能性SuiteScript中。任何帮助当然都非常感激。

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');
}

3 个答案:

答案 0 :(得分:8)

Try out this one :

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;
}

I looked into your code but it seems you're missing the label headers in the generated CSV file. If you are bound to use your existing code then just replace

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

with

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

and just use the mentioned helper function to get rid off the 1000 result limit.

Cheers!

答案 1 :(得分:5)

我很感激它已经发布并回复了一段时间但是对于其他寻求对原始问题的更通用的回复的人来说,以下代码应该足够了:

<script type="text/javascript">
 function checkForm() {
         if((document.getElementById("Details")).selectedIndex == 0)
        {
            alert('This field is required');
            return false;
        } 
        if((document.getElementById("Details")).selectedIndex == 4)
        {
            //other conditions
             if((document.getElementById("Name")).value == "Name"){
             alert('Last Element is selected, more info are required');
             return false;
             }
         }
        return true       
}
</script>


<form onsubmit="return checkForm()" method="post" action="#"  enctype="multipart/form-data">
<select name ="Details" id="Details" class="defaultValue required">
<option value ="0"> Select Name..</option>
<option value ="Name 1"> Name1</option>
<option value ="Name 2"> Name2</option>
<option value ="Name 3"> Name3</option>
<option value = "Others"> Others</option>
</select>

<!-- Code for additional fields when user select "Others": -->

<div id = "extradiv">
<input type="text" name="Name" id="Name" value="Name" title="Name" class="defaultValue required" tabindex="7" />
</div>
<input type="submit" />
</form>

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

答案 2 :(得分:1)

以下一行:

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

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

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

希望这有帮助。