Tom Hanson
Tom Hanson

Reputation: 935

SuiteScript 2.0 search.create returning empty results

I am trying to get all the sales orders which are pending fulfilment at the point of create a new sales order. This is to alert the user that a sales order is already in progress. The code I am using is as such;

    function saleOrderInPendingFulfillmentAlreadyExists(scriptContext)
    {
        //Check if the customer already has an order which is pending fulfillment
        debugger;
        var idCustomer = scriptContext.currentRecord.getValue('entity');
        var recCustomer;
        if(idCustomer)
        {
            var filters = [
               ["mainline", "is", "T"], "and",
               ["type", "anyOf", search.Type.SALES_ORDER], "and",
               ["entity", "anyOf", idCustomer],
           ];

           var columns = [ "tranid" ];

           var searchQuery = search.create({
               "type": search.Type.TRANSACTION,
               "filters": filters,
               "columns": columns
           });

           var salesorders = searchQuery.run().getRange({"start": 0, "end": 1000}) || [];

        }
    }

When debugging the code, the variable salesorders is an empty array yet I know the customer file has at least 8 Sales Orders on it. What am I doing wrong?

Upvotes: 1

Views: 1852

Answers (1)

michoel
michoel

Reputation: 3783

Your problem is that type search filter expects the record in a different format to the search.Type enum ('SalesOrd' instead of 'salesorder'). Welcome to NetSuite!

You can change the filters to

var filters = [
   ["mainline", "is", "T"], "and",
   ["type", "anyOf", 'SalesOrd'], "and",
   ["entity", "anyOf", idCustomer],
];

or, even better

var searchQuery = search.create({
   "type": search.Type.SALES_ORDER,
   "filters": filters,
   "columns": columns
});

BTW, you should definitely look into the NetSuite Search Export Chrome Extension, which lets you build a search in the UI and converts it automatically to SuiteScript.

Upvotes: 3

Related Questions