NetSuite SuiteTalk API - 获取库存详细信息

NetSuite SuiteTalk API - Get Inventory Details

我正在使用 NetSuite 的 SuiteTalk (API) 服务来检索程序集列表。我需要在结果中加载 InventoryDe​​tails 字段以查看分配给项目的 serial/lot 编号。这是我正在使用的当前代码,但结果仍然显示这些字段返回为 NULL,尽管我可以看到 AssemblyBuild 对象的其他字段。如何在交易搜索中将库存详细信息(serials/lot#'s)获取到 return?

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

经过千辛万苦,我终于用下面的代码解决了这个问题:

    /// <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 中查看数据

  • 在保存的搜索中,使用标准主行 = true/false 从主记录(UI 屏幕顶部)和行项目(屏幕底部)读取数据