Sample Search Code

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.

This entry was posted in Beginning with the PHPToolkit, Netsuite PHPToolkit and tagged , , , . Bookmark the permalink.

11 Responses to Sample Search Code

  1. Shane says:

    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!

  2. Marianne Mason says:

    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?

    • daniel says:

      I haven’t tried saved searches myself, but if I do, I will be sure to post some examples. Thanks for the comment.

      • Marianne Mason says:

        thanks. if I come up with a workable piece of code I’ll send it to you.

        • Marianne Mason says:

          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
          }

  3. maysam says:

    hi

    can you also help me with searching based on custom fields?

    thanks

  4. Pavan says:

    Thanks man. This was super helpful!!!!

  5. tristan says:

    hi,

    i tried this and I get a 500 internal server error since I put

    $service->setSearchPreferences(true, 1000, true);

    I wanted to get all results from my search and it is not getting me all results because of the error

    • daniel says:

      The code example in this page is from on old version of the PHP Toolkit. The command has probably changed slightly for the most recent version. Although there is a lot in common, the new toolkit requires slightly new syntax.

  6. Geet says:

    Hi Everybody,

    How do I get the Custom Fields and Customers in list format using Php?

    If anyone have any idea then please share your ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>