The ability to run searches is a great feature of Netsuite Backend. Following is some sample search code. Keep in mind that Netsuite changes the structure of the returned object if their is only one result. (often, rather than returning an array of size 1 containing the object, they just return the object) Also remember that the valid operators will vary dependent upon the field you are searching. In the following example I am searching for a customer by email.
$customerSearch = new nsComplexObject('CustomerSearchBasic'); $customerSearch->setFields(array( "email" => array( "operator" => "is", "searchValue" => $email ) )); $myNSclient->setSearchPreferences(false, 1000); try { $response = $myNSclient->search($customerSearch); }catch(Exception $e){ //do something } /* @var $response nsSearchResponse */ if ($response->isSuccess == true) { $totalRecords = (int) $response->totalRecords; if ($totalRecords == 0) { //nothing was found } $recordList = $response->recordList; if ($totalRecords == 1) { $record = $recordList[0]; //$record is a nsComplexObject of type customer } else if ($totalRecords > 1) { foreach ($recordList as $record) { //$record is a nsComplexObject of type customer } } }else{ print_r($response);//something went wrong, see the error message }In this example I could have used any of the following operators:
- contains
- doesNotContain
- doesNotStartWith
- empty
- hasKeywords
- is
- isNot
- notEmpty
- startsWith
For details on which operators are valid for various field types, see “platform core > types” under the schema browser.

Thank you for this post!!!! Saved my hours of searching… All i needed was that little operator “is”. Why cant NetSuite publish this an make this easy. In any case, thanks again!
I’m glad it was useful
Thanks for posting info; I’ve been working with the toolkit for a bit now but still having examples is very, very useful. Have you done anything retrieving the results of a saved search?
I haven’t tried saved searches myself, but if I do, I will be sure to post some examples. Thanks for the comment.
thanks. if I come up with a workable piece of code I’ll send it to you.
Daniel -
this works
require_once ‘PHPtoolkit.php’;
require_once ‘login_info.php’;
global $myNSClient;
$id = ‘saved search ID’;
$item = new nsComplexObject(‘SearchStringField’);
$item->setFields(array( ‘searchValue’ => $id,
‘operator’ => ‘is’));
$searchItem = array (
‘savedSearchScriptId’ => $item
);
//object type would be replaced with the correct type
$search = new nsComplexObject(‘TransactionSearchAdvanced’);
$search->setFields($searchItem );
//from this point down, this is your code
$myNSclient->setSearchPreferences(false, 10);
try {
$response = $myNSclient->search($search);
}
catch(Exception $e){
echo $e;
die(“problem with memory”);
}
if ($response->isSuccess == true) {
$totalRecords = (int) $response->totalRecords;
if ($totalRecords == 0) {
echo “nothing found\n”;
} else {
$recordList = $response->recordList;
if ($totalRecords == 1) {
$record = $recordList[0];
var_dump($record);
} else {
// foreach ($recordList as $record) {
// var_dump($record);
// }
echo “$totalRecords found\n”;
}
}
}else{
print_r($response);//something went wrong, see the error message
}
hi
can you also help me with searching based on custom fields?
thanks