2016-11-25 57 views
0

我有這樣小的代碼如何優化代碼中suitelet運行大量數據的NetSuite

function suitelet(request, response){ 


     var fromIndex = 0; 
     var toIndex = 500; 
     var totalItems= 0; 


     var itemSearch = nlapiCreateSearch("item", 
       [ 
       ["formulanumeric: {quantityavailable} - {reorderpoint}","lessthanorequalto","0"], 
       "AND", 
       ["parent","noneof","@[email protected]"] 
       "AND", 
       ["locationquantityavailable","greaterthan","0"] 


     ], 
       [ 
        new nlobjSearchColumn("itemid",null,null).setSort(false), 
        new nlobjSearchColumn("parent",null,null), 
        new nlobjSearchColumn("othervendor",null,null), 
        new nlobjSearchColumn("department",null,null), 
        new nlobjSearchColumn("location",null,null) 

       ] 
     ); 

     var resultSet = itemSearch.runSearch();    

     var results = resultSet.getResults(fromIndex,toIndex);   

     while(results != null && results.length > 0) 
     { 
      totalItems += results.length; 
      fromIndex = toIndex; 
      toIndex += 500; 
      results = resultSet.getResults(fromIndex,toIndex); 
     } 

     response.write(totalItems); 


    } 

當我使用該過濾器的問題是[「locationquantityavailable」,「大於」,「0」]它正在工作,因爲結果只有400,並且打印總數。

因爲結果是20000

如何使之成爲大數據的工作,當我取出那張無限和加載在瀏覽器和數據永遠不會顯示的過濾器

。我正在使用沙箱帳戶。

回答

0

由於您的代碼只計算總數,因此不需要檢索所有數據。只要保存的搜索返回總數。

下面是基本的想法(我沒有測試代碼,所以可能有一些錯誤,但總的想法應該工作)。

function suitelet(request, response) { 

    var results = nlapiSearchRecord('item', null, 
     [ 
      ["formulanumeric: {quantityavailable} - {reorderpoint}","lessthanorequalto","0"], "AND", 
      ["parent","noneof","@[email protected]"] 
     ], 
     [ 
      new nlobjSearchColumn("formulanumeric",null,'GROUP').setFormula('1'), 
      new nlobjSearchColumn('itemid',null,'COUNT') 
     ] 
    ); 

    if(results) { 
     var totalItems = results[0].getValue('itemid',null,'COUNT'); 
     response.write(totalItems); 
    } 

} 
+0

不錯,但我想用nlapiCreateSearch,計算總只是一個例子。我也想要其他數據的行..這是工作400記錄,但是當有更多的項目像20000它不工作.. –

+1

你到底想要完成什麼? – erictgrubaugh

1

試試這個代碼,我添加了一個函數來獲取所有的結果。請小心,因爲可能有其他方式可以優化您的搜索。另外,請記住Suitelets具有超時設置(30秒)。

function suitelet(request, response) { 
    var itemSearch = nlapiCreateSearch("item", 
      [ 
       ["formulanumeric: {quantityavailable} - {reorderpoint}", "lessthanorequalto", "0"], 
       "AND", 
       ["parent", "noneof", "@[email protected]"] 
       "AND", 
       ["locationquantityavailable", "greaterthan", "0"] 

      ], 
      [ 
       new nlobjSearchColumn("itemid", null, null).setSort(false), 
       new nlobjSearchColumn("parent", null, null), 
       new nlobjSearchColumn("othervendor", null, null), 
       new nlobjSearchColumn("department", null, null), 
       new nlobjSearchColumn("location", null, null) 

      ]); 

    var results = fullSearch(itemSearch); 
    var totalItems = results.length; 
    response.write(totalItems); 
} 

function fullSearch(search) { 
    var resultset = search.runSearch(); 
    var resultsets = []; 
    var returnSearchResults = []; 
    var searchid = 0; 
    var startdate, enddate, resultslice; 
    do { 
     resultslice = getResultSlice(resultset, searchid); 
     for (rs in resultslice) { 
      returnSearchResults.push(resultslice[rs]); 
      searchid++; 
     } 
    } while (resultslice.length == 1000); 

    return returnSearchResults; 

    //*********** HELPER FUNCTION ***********/ 
    function getResultSlice(resultset, searchid) { 
     var resultslice = resultset.getResults(searchid, searchid + 1000); 
     return resultslice; 
    } 
} 
1

你可能真的不希望你的用戶必須通過20K涉水rows.If你的使用情況僅僅是創建一個搜索並顯示搜索結果(例如,使用戶可以導出,或進一步發揮與結果,但不檢查一些箱子重新提交進一步處理),那麼你可以使用nlobjSearch.setRedirectURLToSearchResults();

eg

var itemSearch = nlapiCreateSearch("item", 
      [ 
      ["formulanumeric: {quantityavailable} - {reorderpoint}","lessthanorequalto","0"], 
      "AND", 
      ["parent","noneof","@[email protected]"] 
      "AND", 
      ["locationquantityavailable","greaterthan","0"] 


    ], 
      [ 
       new nlobjSearchColumn("itemid",null,null).setSort(false), 
       new nlobjSearchColumn("parent",null,null), 
       new nlobjSearchColumn("othervendor",null,null), 
       new nlobjSearchColumn("department",null,null), 
       new nlobjSearchColumn("location",null,null) 

      ] 
    ); 
    itemSearch.setRedirectURLToSearchResults(); 

現在NetSuite的保存的搜索窗格將處理分頁等