Douglas Wiley
Douglas Wiley

Reputation: 523

How to create NetSuite SuiteTalk search with multiple terms?

How does one create a SuiteTalk (NetSuite web api) search query specifying multiple search terms indicating a logical OR operator?

For example, I want to retrieve TimeBill records whose create OR last modified dates are within a particular range. Here's my code that works great for a single search term. Simply adding another search term appears to create a logical AND operation.

    /// <summary>
    /// Return the list of time bills whose last modified date is within 
    /// the indicated date range.
    /// </summary>
    /// <param name="from">Required from date</param>
    /// <param name="to">Optional to date</param>
    /// <returns>List of time bills</returns>
    public IEnumerable<TimeBill> GetTimeBills(DateTime from, DateTime to)
    {
        _log.Debug(String.Format("Enter TimeBill(DateTime from='{0}', DateTime to='{1}')", from, to));

        // Build search criteria.
        TimeBillSearch search = new TimeBillSearch();
        TimeBillSearchBasic searchBasic = new TimeBillSearchBasic();
        SearchDateField searchDateField = new SearchDateField();
        searchDateField.@operator = SearchDateFieldOperator.within;
        searchDateField.operatorSpecified = true;
        searchDateField.searchValue = from;
        searchDateField.searchValueSpecified = true;
        searchDateField.searchValue2 = to;
        searchDateField.searchValue2Specified = true;
        searchBasic.dateCreated = searchDateField;            
        search.basic = searchBasic;

        return this.Get<TimeBill>(search);
    }  

    /// <summary>
    /// Perform a paged search and convert the returned record to the indicated type.
    /// </summary>
    private IEnumerable<T> Get<T>(SearchRecord searchRecord)
    {
        _log.Debug("Enter Get<T>(SearchRecord searchRecord)");

        // This is returned.
        List<T> list = new List<T>();

        // The suitetalk service return this.
        SearchResult result = null;

        using (ISuiteTalkService service = SuiteTalkFactory.Get<SuiteTalkService>())
        {
            do
            {
                // .search returns the first page of data.
                if (result == null)
                {
                    result = service.search(searchRecord);
                }
                else // .searchMore returns the next page(s) of data.
                {
                    result = service.searchMoreWithId(result.searchId, result.pageIndex + 1);
                }

                if (result.status.isSuccess)
                {
                    foreach (Record record in result.recordList)
                    {
                        if (record is T)
                        {
                            list.Add((T)Convert.ChangeType(record, typeof(T)));
                        }
                    }
                }
            }
            while (result.pageIndex < result.totalPages);
        }
        return list;
    }

Upvotes: 4

Views: 6670

Answers (1)

mk12
mk12

Reputation: 169

Per the NetSuite User Community (forum), I do not believe this is currently possible (at least it does not appear in the WSDL for SuiteTalk/Web Services): https://usergroup.netsuite.com/users/showthread.php?t=29818

However, you can dynamically create a complex Saved Search (using SuiteScript in a Suitelet) that includes AND/OR and parentheses by using the nlobjSearchFilter methods setLeftParens(), setRightParens(), and setOr() (as you surmised, a logical "AND" is the default behavior when multiple filters are present).

A dynamically created Saved Search (confusing terminology here, I know) can also be saved and loaded for later reuse. You could therefore potentially take advantage of dynamically created Saved Searches on the NetSuite server by having your Web Services code invoke a Saved Search and retrieve the results, yet still have everything be dynamic (no hardcoded search filters/values).

As mentioned on the Forum, you could also potentially execute multiple searches and stitch the results together yourself (in your SuiteTalk C#/Java etc. code).

Upvotes: 1

Related Questions