littlegreendude
littlegreendude

Reputation: 117

How do I search for NetSuite inventory items using a custom field?

Using NetSuite 2020.1 web service, I would like to search for an inventory item by it's name and catalog code (custom field).

Here is a screen shot of the search behavior I'm trying to replicate through code:

enter image description here

And here is where I'm at code wise(C#):

private string Exists(MapInvItem mapInvItem, ref InventoryItem inventoryItem)
    {
        string sRetrunValue = ITEM_NOTFOUND;

        ItemSearch invSearch = new ItemSearch();           

        SearchCustomField[] searchCustomFields = new SearchCustomField[]
        {
            new SearchStringCustomField()
            {
                @operator = SearchStringFieldOperator.@is,
                operatorSpecified = true,
                searchValue = mapInvItem.catalogcode,
                scriptId = CATALOG_CODE_NSID
            }
        };

        SearchStringField name = new SearchStringField();
        name.@operator = SearchStringFieldOperator.@is;
        name.operatorSpecified = true;
        name.searchValue = mapInvItem.sku;

        ItemSearchBasic invBasic = new ItemSearchBasic();
        invBasic.displayName = name;
        invBasic.customFieldList = searchCustomFields;            

        invSearch.basic = invBasic;

        SearchResult result = Client.Service.search(invSearch);

        if (result.status.isSuccess)
        {
            if (result.totalRecords == 1)
            {
                // one item found => good to go
                Record[] records = result.recordList;
                inventoryItem = (InventoryItem)records[0];
                sRetrunValue = inventoryItem.internalId;
            }
            else if (result.totalRecords > 1)
            {
                // more than one item, we may have a problem
                Record[] records;
                List<InventoryItem> inventoryItems = new List<InventoryItem>();
                // Loop thru page count
                for (int lpc = 0; lpc <= result.totalPages - 1; lpc++)
                {
                    records = result.recordList;
                    for (int lcv = 0; lcv <= records.Length - 1; lcv++)
                    {
                        inventoryItems.Add((InventoryItem)records[lcv]);
                        return ((InventoryItem)records[lcv]).internalId;
                    }
                }
            }
        }
        else
        {
            // log failure
        }

        return sRetrunValue;
    }

Note, for result.status.isSuccess I'm getting true, but totalRecords is zero.

Upvotes: 2

Views: 1530

Answers (1)

markcdev
markcdev

Reputation: 36

Use itemId instead of displayName invBasic.itemId = name;

Upvotes: 2

Related Questions