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

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中查看数据
在“保存的搜索”中,使用Criteria Main Line = true / false来从主记录(UI屏幕顶部)和订单项(屏幕底部)读取数据

关于netsuite - NetSuite SuiteTalk API-获取库存详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39004193/

10-12 15:15