2016-08-17 81 views
4

我正在使用NetSuite的SuiteTalk(API)服務來檢索組件列表。我需要在結果中加載InventoryDe​​tails字段以查看分配給這些項目的序列號/批號。這是我正在使用的當前代碼,但結果仍然顯示這些字段將返回爲NULL,儘管我可以看到AssemblyBuild對象的其他字段。如何獲取庫存詳細信息(連續出版物/批次編號)以便在事務處理搜索時返回?NetSuite SuiteTalk API - 獲取庫存詳細信息

public static List<AssemblyBuildResult> Get() 
{ 
    var listAssemblyBuilds = new List<AssemblyBuildResult>(); 

    var service = Service.Context(); 

    var ts = new TransactionSearch(); 
    var tsb = new TransactionSearchBasic(); 

    var sfType = new SearchEnumMultiSelectField 
    { 
     @operator = SearchEnumMultiSelectFieldOperator.anyOf, 
     operatorSpecified = true, 
     searchValue = new string[] { "_assemblyBuild" } 
    }; 

    tsb.type = sfType; 
    ts.basic = tsb; 
    ts.inventoryDetailJoin = new InventoryDetailSearchBasic(); 

    // perform the search 
    var response = service.search(ts); 
    response.pageSizeSpecified = true; 

    // Process response 
    if (response.status.isSuccess) 
    { 
     // Process the records returned in the response 
       // Get more records with pagination 
       if (response.totalRecords > 0) 
       { 
        for (var x = 1; x <= response.totalPages; x++) 
        { 
         var records = response.recordList; 

         foreach (var t in records) 
         { 
          var ab = (AssemblyBuild) t; 
          listAssemblyBuilds.Add(GetAssemblyBuildsResult(ab)); 
         } 

         if (response.pageIndex < response.totalPages) 
         { 
          response = service.searchMoreWithId(response.searchId, x + 1); 
         } 
        } 
       } 
      } 

      // Parse and return NetSuite WorkOrder into assembly WorkOrderResult list 
      return listAssemblyBuilds; 
     } 

回答

3

多的痛苦和折磨後,我能解決這個問題,下面的代碼:

/// <summary> 
    /// Returns List of AssemblyBuilds from NetSuite 
    /// </summary> 
    /// <returns></returns> 
    public static List<AssemblyBuildResult> Get(string id = "", bool getDetails = false) 
    { 
     // Object to populate and return results 
     var listAssemblyBuilds = new List<AssemblyBuildResult>(); 

     // Initiate Service and SavedSearch (TransactionSearchAdvanced) 
     var service = Service.Context(); 
     var tsa = new TransactionSearchAdvanced 
     { 
      savedSearchScriptId = "customsearch_web_assemblysearchmainlist" 
     }; 

     // Filter by ID if specified 
     if (id != "") 
     { 
      tsa.criteria = new TransactionSearch() 
      { 
       basic = new TransactionSearchBasic() 
       { 
        internalId = new SearchMultiSelectField 
        { 
         @operator = SearchMultiSelectFieldOperator.anyOf, 
         operatorSpecified = true, 
         searchValue = new[] { 
         new RecordRef() { 
          type = RecordType.assemblyBuild, 
          typeSpecified = true, 
          internalId = id 
         } 
        } 
        } 
       } 
      }; 

     } 

     // Construct custom columns to return 
     var tsr = new TransactionSearchRow(); 
     var tsrb = new TransactionSearchRowBasic(); 

     var orderIdCols = new SearchColumnSelectField[1]; 
     var orderIdCol = new SearchColumnSelectField(); 
     orderIdCols[0] = orderIdCol; 
     tsrb.internalId = orderIdCols; 

     var tranDateCols = new SearchColumnDateField[1]; 
     var tranDateCol = new SearchColumnDateField(); 
     tranDateCols[0] = tranDateCol; 
     tsrb.tranDate = tranDateCols; 

     var serialNumberCols = new SearchColumnStringField[1]; 
     var serialNumberCol = new SearchColumnStringField(); 
     serialNumberCols[0] = serialNumberCol; 
     tsrb.serialNumbers = serialNumberCols; 

     // Perform the Search 
     tsr.basic = tsrb; 
     tsa.columns = tsr; 
     var response = service.search(tsa); 

     // Process response 
     if (response.status.isSuccess) 
     { 
      var searchRows = response.searchRowList; 
      if (searchRows != null && searchRows.Length >= 1) 
      { 
       foreach (SearchRow t in searchRows) 
       { 
        var transactionRow = (TransactionSearchRow)t; 
        listAssemblyBuilds.Add(GetAssemblyBuildsResult(transactionRow, getDetails)); 
       } 
      } 
     } 

     // Parse and return NetSuite WorkOrder into assembly WorkOrderResult list 
     return listAssemblyBuilds; 
    } 

    private static string GetAssemblyBuildLotNumbers(string id) 
    { 
     var service = Service.Context(); 
     var serialNumbers = ""; 

     var tsa = new TransactionSearchAdvanced 
     { 
      savedSearchScriptId = "customsearch_web_assemblysearchlineitems" 
     }; 

     service.searchPreferences = new SearchPreferences { bodyFieldsOnly = false }; 
     tsa.criteria = new TransactionSearch() 
     { 
      basic = new TransactionSearchBasic() 
      { 
       internalId = new SearchMultiSelectField 
       { 
        @operator = SearchMultiSelectFieldOperator.anyOf, 
        operatorSpecified = true, 
        searchValue = new[] { 
         new RecordRef() { 
          type = RecordType.assemblyBuild, 
          typeSpecified = true, 
          internalId = id 
         } 
        } 
       } 
      } 
     }; 

     // Construct custom columns to return 
     var tsr = new TransactionSearchRow(); 
     var tsrb = new TransactionSearchRowBasic(); 

     var orderIdCols = new SearchColumnSelectField[1]; 
     var orderIdCol = new SearchColumnSelectField(); 
     orderIdCols[0] = orderIdCol; 
     tsrb.internalId = orderIdCols; 

     var serialNumberCols = new SearchColumnStringField[1]; 
     var serialNumberCol = new SearchColumnStringField(); 
     serialNumberCols[0] = serialNumberCol; 
     tsrb.serialNumbers = serialNumberCols; 

     tsr.basic = tsrb; 
     tsa.columns = tsr; 

     var response = service.search(tsa); 

     if (response.status.isSuccess) 
     { 
      var searchRows = response.searchRowList; 
      if (searchRows != null && searchRows.Length >= 1) 
      { 
       foreach (SearchRow t in searchRows) 
       { 
        var transactionRow = (TransactionSearchRow)t; 
        if (transactionRow.basic.serialNumbers != null) 
        { 
         return transactionRow.basic.serialNumbers[0].searchValue; 
        } 
       } 
      } 
     } 

     return serialNumbers; 
    } 

    private static AssemblyBuildResult GetAssemblyBuildsResult(TransactionSearchRow tsr, bool getDetails) 
    { 
     if (tsr != null) 
     { 
      var assemblyInfo = new AssemblyBuildResult 
      { 
       NetSuiteId = tsr.basic.internalId[0].searchValue.internalId, 
       ManufacturedDate = tsr.basic.tranDate[0].searchValue, 
       SerialNumbers = tsr.basic.serialNumbers[0].searchValue 

      }; 

      // If selected, this will do additional NetSuite queries to get detailed data (slower) 
      if (getDetails) 
      { 
       // Look up Lot Number 
       assemblyInfo.LotNumber = GetAssemblyBuildLotNumbers(tsr.basic.internalId[0].searchValue.internalId); 
      } 

      return assemblyInfo; 
     } 

     return null; 

    } 

我學到了什麼有關從NetSuite的拉動數據:

  • 使用SavedSearches被提取API對象中不會自動生成的數據的最佳方法
  • 幾乎不支持
  • 不要在SavedSearch上指定一個ID,在TransactionSearch中指定一個條件以獲得一條記錄
  • 您需要指定實際下拉的列。 NetSuite的不只是讓你從一個SavedSearch數據自動

  • 在包含分組

  • 在保存的搜索一個SavedSearch無法查看數據,使用標準幹線=真/假讀取數據從主要記錄(UI屏幕頂部)和訂單項(屏幕底部)