Search code examples
c#netsuitesuitetalk

Count SalesOrder Items more elegantly in SuiteTalk


I'm working on creating the backend for a NetSuite-based application using SuiteTalk. I'm trying to count the total number of items in SalesOrders.

My issue is that, when performing a search operation to get all SalesOrders, the Record[] object returned loses precision when cast to a SalesOrder[] (using Array.ConvertAll(...) and manually). In my case, this means getting a Null Reference exception when trying to access the itemList field.

My eventual solution was as follows:

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

However this calls a get request for every single Sales Order, meaning it takes an age and is very inelegant. Is there a more elegant solution I'm missing? Has anyone else had a similar issue or am I being dense?

The class definition for Record is:

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

The class definition for SalesOrder is several thousand lines of code, but the SchemaBrowser entry is fairly clear.


Solution

  • I have found an answer to my question here.

    Essentially, to prevent the itemList field from becoming null, you have to set the searchPreferences.bodyFieldsOnly field to false. This now runs in 1/10th the time of my solution.