Getting a list of all customers from Netsuite using the php toolkit

No Gravatar

Netsuite is a web-based accounting and business process system that has garnered some praise over the last few years for it’s flexibility and customizability. And while it’s true that the interface allows you to customize many things, many business processes still require other (non-Netsuite) systems to work. For that reason, integration is important. Fortunately, Netsuite provides a robust API framework for getting data out of the system as well as uploading data to the system. Unfortunately, if your language of choice is php, the documentation around this API is a little sparse. This article is meant to give you a simple example that was not well defined in the Netsuite php documentation: how to get a complete list of customers.

In my search for the best documentation around the Netsuite API, at least with regards to PHP, I’ve found a number of online resources that will be helpful. They are listed at http://www.delicious.com/adamhaeder/netsuite.

To begin, you must first download the PHPToolkit from http://www.netsuite.com/portal/developers/resources/suitetalk-sample-applications.shtml. The article assumes that you have done this and that you’ve updated the ‘login_info.php’ file with the appropriate username and password.

// we're assuming these 2 files are in the same directory, if not, change these paths
require_once 'PHPtoolkit.php';
require_once 'login_info.php';
// how many records do you want to get per search? Max is 999.
// Too few, and this scripts runs way too long. Too many,
// and you tax the netsuite server and could have timeouts.
$searchmax="500";
global $myNSclient;
$myNSclient->setSearchPreferences(false, $searchmax);
$customerSearchParam = new nsComplexObject('SearchStringField');
// here is where we cheat. Ideally, we could just use
// the 'getAll' function on the
// 'Customer' object, but Netsuite doesn't support that
// (see http://bit.ly/p0JKhl)
// Instead, we have to search for a 'catch-all' value
// to return all results
$customerSearchParam->setFields(array('searchValue' => '', 'operator' => 'contains'));
$customerSearchBasic = new nsComplexObject("CustomerSearchBasic");
$customerSearchBasic->setFields(array ('companyName'=> $customerSearchParam));

// Do the first search
$searchResponse = $myNSclient->search($customerSearchBasic);

if ($searchResponse->isSuccess)
{
 // how many records did we get back?
 $totalRecords = $searchResponse->totalRecords;
 foreach ( $searchResponse->recordList as $record )
 {
  $sugarid="";
  $internalId = $record->getField("internalId");
  $companyName = $record->getField("companyName");

  // you only have to do this part if you want to display a custom field
  // that you've created for the customer. The data returned from
  // the search above does not include custom fields, so if you
  // want to see them, you have to do another search, specifically
  // for this customer id, to get all the custom fields, and then
  // just display the one you want. This is expensive, because you
  // have to do a netsuite search for each company, but it's the
  // only way I know how to do it.
  // If you don't want to see custom fields, remove this part
  // and the script will run much faster. In this case, the
  // custom field I want to see is 'custentitysugar_org_id'
  $customerSearch = new nsRecordRef( array( "type" => "customer", "internalId" => $internalId));
  $getResponse = $myNSclient->get($customerSearch);

  foreach ( $getResponse->record->getField('customFieldList')->getField('customField') as $customField )
  {
   if ( $customField->getField('internalId') == "custentitysugar_org_id" )
   {
    $sugarid = $customField->getField('value');
   }
  }
  // output the value of the 3 fields we want to
  // see: Netsuite ID, Company name and Sugar ID
  echo "$internalId;$companyName;$sugarid\n";
 }
}

$searchId = $searchResponse->searchId;

$currentPage = $searchResponse->pageIndex;
$pageSize = $searchResponse->pageSize;

// how many more searches do we need to do in order to get all the results?
$number_of_searches = ceil ($totalRecords / $searchmax);
echo "DEBUG: Doing $number_of_searches searches\n";

// Now we do the same thing over again, getting chunks
// of companies 500 at a time. The only difference is that
// we're calling 'searchMorewithID' now instead of 'search'
for ($i = 2; $i <= $number_of_searches; $i++ )
{
 echo "DEBUG: Search $i\n";
 $searchResponse2 = $myNSclient->searchMorewithId($searchId,$i);
 if ($searchResponse2->isSuccess)
 {
  foreach ( $searchResponse2->recordList as $record )
  {
   $sugarid="";
   $internalId = $record->getField("internalId");
   $companyName = $record->getField("companyName");

   $customerSearch = new nsRecordRef( array( "type" => "customer", "internalId" => $internalId));
   $getResponse = $myNSclient->get($customerSearch);

   foreach ( $getResponse->record->getField('customFieldList')->getField('customField') as $customField )
   {
    if ( $customField->getField('internalId') == "custentitysugar_org_id" )
    {
     $sugarid = $customField->getField('value');
    }
   }

   echo "$internalId;$companyName;$sugarid\n";
  }
 }
}

This script was designed to be run from the command line. Save the script as ‘list_all_customers.php’ and then run:

$ php5 ./list_all_customers.php > all_netsuite_customers.txt

You should end up with a file that has lines that look like this:

178;Alexander Open Systems;
179;21CSI;e35eba7a-fa90-a4a7-6561-478b722ac8ed
181;A Well Dressed Window;12723f15-eadd-69e1-718e-4b8c15c82632
182;ABC Seamless Siding;38097b08-af8b-98c4-70db-478b71814eb9
183;Able2Know.org;

Again, because of the custom field query we have to do for each customer, this script takes some time to run. Currently, I’m pulling back 1700 customers, and the script takes about 11 minutes to run.

Hopefully this has been helpful to those of you new to php scripting with Netsuite.

2 Responses to “Getting a list of all customers from Netsuite using the php toolkit”

  • swninetails

    I love your post!!! Thank you!!!

    I have been trying to teach myself how to integrate with netsuite, seems like I must be missing something… The goal is to return a set of customer records with an entity status condition, for instance give me all customers with a ‘closed’ status, below are the two ways I have attempted this but I must be missing something, any help would be terrific!

    Try 1
    $RecordRef = new nsRecordRef(array(‘entitystatus’ => 17, ‘type’ => ‘customer’));
    $searchResponse = $myNSclient->get($RecordRef);

    Try 2
    $itemIdSearchField = new nsComplexObject(“SearchMultiSelectField”);
    $itemIdSearchField->setFields(array(“searchValue” => new nsListOrRecordRef(array(‘internalId’ => $itemId)),”operator” => “anyOf”));
    $itemSearch = new nsComplexObject(“CustomerSearch”);
    $itemSearch->setFields(array(“entitystatus” => $itemIdSearchField));
    $searchResponse = $myNSclient->search($itemSearch);

    Thank you for any suggestions…

  • nirav patel

    Currently I am working on integration of netsuite using the phptoolkit.

    I am facing problem in calculating the tax while submitting the sales order in netsuite. Can you please guide me how to get proper tax code base on shipping address. and after receiving the tax code how can I pass in netsuite request.

    Thanks in advance.

Leave a Reply