在SuiteTalk中更优雅地计算SalesOrder项目

时间:2019-07-05 09:27:50

标签: c# netsuite suitetalk

我正在使用SuiteTalk为基于NetSuite的应用程序创建后端。我正在尝试计算SalesOrders中的项目总数。

我的问题是,执行搜索操作以获取所有SalesOrders时,返回的Record[]对象在转换为SalesOrder[]时(使用Array.ConvertAll(...)并手动转换)会失去精度。就我而言,这意味着在尝试访问itemList字段时会得到Null Reference异常。

我最终的解决方法如下:

for (SearchResult searchResult = _service.search(salesOrderSearch); searchResult.pageIndex <= searchResult.totalPages; searchResult = _service.searchNext()) {

                if (searchResult.status.isSuccess) {

                    // Convert Record[] to SalesOrder[]. Note that direct conversion causes some fields to become Null 
                    SalesOrder[] salesOrdersNull = Array.ConvertAll(searchResult.recordList, item => (SalesOrder)item);

                    // To avoid the null issue, send a get request for each order by its internalId
                    foreach (SalesOrder salesOrderNull in salesOrdersNull) {
                        SalesOrder salesOrder = getSalesOrder(salesOrderNull.internalId);

                        // Increment the count by the total number of items
                        count += salesOrder.itemList.item.Length;
                    }
                } else {
                    string errorCodes = Helpers.generateErrorString(searchResult.status.statusDetail);
                    throw new SuiteTalkServiceException("count Sales Order Lines. Failed with error code(s) " + errorCodes);
                }
            }

但是,这需要为每个销售订单调用get请求,这意味着要花很多时间并且非常不雅致。我还缺少更优雅的解决方案吗?还有其他人遇到过类似的问题吗?还是我很忙?

Record的类定义为:

public abstract partial class Record {

        private string[] nullFieldListField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("name", IsNullable=false)]
        public string[] nullFieldList {
            get {
                return this.nullFieldListField;
            }
            set {
                this.nullFieldListField = value;
            }
        }
    }

SalesOrder的类定义是几千行代码,但是SchemaBrowser entry很清楚。

1 个答案:

答案 0 :(得分:1)

我找到了问题here的答案。

从本质上说,为防止itemList字段变为null,必须将searchPreferences.bodyFieldsOnly字段设置为false。现在,它的运行时间是我的解决方案时间的1/10。