Netsuite Toolkit: Finding an item in a saved search

Thanks to my job, I’ve had to learn a little bit about the accounting software known as Netsuite. All but 1 of our engineers say it’s name with a lot of cuss words. I’m kind of 50-50. Since the one engineer has left my group, I’ve had to get even more familiar with it. Anything NS related is usually way more backend than I’m comfortable with but I’ve gotten used to it. As long as I have a sandbox to play with, I can’t screw up anyone too much. …I hope.

Anywho, NS has a PHPToolkit for developers. It consists of a lot of classes and methods that are used with common NS integrated packages to speak to the NS API. Note: I am NO Netsuite expert. I feel like I’m stumbling around and good documentation is rare. I just wanted to add a little bit to the knowledge bank.

Back to what this post is about. I needed to find a specific item in a Netsuite saved search to check if anything was different between item A and item B. I saw a lot of articles about using ItemSearchBasic() to find an item. Problem is is that class won’t return what the saved search returns. It just returns all the attributes on the one method. I needed to use ItemSearchAdvanced() which does return the saved search. The answer was to use both.

$service = new NetSuiteService();
$service->setSearchPreferences(false, 20, true);

// The field with the ID I wanted to find the item by.
$itemField = new SearchStringField();
$itemField->searchValue = '021-0254';
$itemField->operator = 'is';

// ItemId is the ID I'm trying to run the search on.
$searchBasic = new ItemSearchBasic();
$searchBasic->itemId = $itemField;

$searchCriteria = new ItemSearch();
$searchCriteria->basic = $searchBasic;

$search = new ItemSearchAdvanced();
$search->savedSearchScriptId = "saved_search_id";
$search->criteria = $searchCriteria;

// Build the search with the criteria and run it.
$request = new SearchRequest();
$request->searchRecord = $search;

$searchResponse = $service->search($request);

echo "SEARCH SUCCESS, records found: " . $searchResponse->searchResult->totalRecords . "<br/>";

This should say “SEARCH SUCCESS, records found: 1” instead of 2,000+ that the search does have.

Instead of looking for an Item by ItemId, you can run a search on other fields. You’ll just need to figure out what to use instead of SearchStringField().

Good luck!