2017-03-31 139 views
0

我試圖根據搜索結果停止創建記錄。儘管我知道數據存在,但我似乎無法通過我的SuiteScript搜索返回任何數據。0從NetSuite搜索返回的結果

我創建了一個自定義保存的搜索,使用下面使用的確切過濾器並返回我正在尋找的結果。

爲什麼我可能沒有檢索到任何結果,有什麼突出嗎?

注意:sfdcAccountId變量確實有一個值,所以我正在搜索一個有效的值。

// 2.0 
define(["N/error", "N/log", "N/search"], function (err, log, search) { 

    /** 
    * User Event 2.0 example showing usage of the Submit events 
    * 
    * @NApiVersion 2.x 
    * @NModuleScope SameAccount 
    * @NScriptType UserEventScript 
    * @appliedtorecord customer 
    */ 
    var exports = {}; 

    function beforeSubmit(scriptContext) {  
     log.debug({ 
      "title": "Before Submit", 
      "details": "action=" + scriptContext.type 
     }); 

     if (doesCustomerExist(scriptContext)) { 
      throw err.create({ 
       "name": "DUPLICATE_SFDC_ACCOUNT_ID", 
       "message": "Customer Already Contains SFDC Account Id", 
       "notifyOff": true 
      }); 
     } 
    } 

    function doesCustomerExist(scriptContext) { 
     var sfdcAccountId = scriptContext.newRecord.getValue('custentitysfdc_account_id'); 
     log.debug({ 
      "title": "Before Submit", 
      "details": "sfdcAccountId=" + sfdcAccountId 
     }); 

     if(sfdcAccountId == null || sfdcAccountId == '') return false; 

     var searchResult = search.create({ 
              type: search.Type.CUSTOMER, 
              filters: ['custentitysfdc_account_id', search.Operator.IS, sfdcAccountId] 
             }).run(); 

     return (searchResult != null && searchResult.length > 0); 
    } 

    exports.beforeSubmit = beforeSubmit; 
    return exports; 
}); 

回答

6

當您在搜索調用.run(),它返回一個search.ResultSet對象。如果您在該對象上調用getRange(),則會得到您要查找的結果數組。這是您的搜索的更新版本,返回search.Result[],您可以根據需要檢查長度或迭代。

var searchResult = search.create({ 
    type: search.Type.CUSTOMER, 
    filters: ['custentitysfdc_account_id', search.Operator.IS, sfdcAccountId] 
}).run().getRange({start: 0, end: 1000});