Boone Putney bio photo

Boone Putney

Software Development
Random Musings
Austin, Texas

HumanPlanet Soleer

Email LinkedIn Github

I recently completed a project linking a local LAMP application with a newly implemented NetSuite deployment. Working with the customer and the NetSuite partner, I have been impressed with the NetSuite system and capabilities. I was surprised, however, by how few examples of recent code were floating around, especially in PHP, so I figured I’d share some of my code.

Below is the first example, there are a few cryptic comments to hopefully help explain what’s going on. If you have any questions, feel free to leave a comment below. Thanks!

Code

netsuite.php:

 1 <?php
 2 require_once 'path/to/PHPToolkit_2014_1/PHPToolkit/NetSuiteService.php';
 3 
 4 /*******************************
 5 * Settings
 6 *******************************/
 7 
 8 $days_to_update = 2; //all records updated after this number of days in the past will be updated
 9 $results_per_page = 50;
10 
11 /*******************************
12 * Search Query
13 *******************************/
14 
15 //get correct data center url
16 $params = new GetDataCenterUrlsRequest();
17 $params->account = "XXXXXXX";
18 $response = $service->getDataCenterUrls($params);
19 $nshost = $response->getDataCenterUrlsResult->dataCenterUrls->webservicesDomain;
20 
21 //setup
22 $service = new NetSuiteService();
23 $service->setSearchPreferences(false, $results_per_page);
24 $search = new CustomerSearchBasic();
25 
26 //date search:  creates a date record referencing all items after $days_to_update ago.
27 //i.e.  if $days_to_update=2 & today is 10/9/2014 XX:XX:XX references all dates after 10/7/2009 XX:XX:XX
28 $searchDateField = new SearchDateField();
29 $searchDateField->operator = "after";
30 $searchDateField->searchValue = date('Y-m-d\TH:i:s.000\Z', strtotime('-'.$days_to_update.' days'));
31 $search->lastModifiedDate = $searchDateField;
32 
33 //subsidiary search
34 //our database has multiple subsidiaries, make sure this customer is associated with the correct one
35 $searchMultiSelectField = new SearchMultiSelectField();
36 $searchValue = new RecordRef();
37 $searchValue->type = 'subsidiary';
38 $searchValue->internalId = 1;
39 setFields($searchMultiSelectField, array('operator' => 'anyOf', 'searchValue' => $searchValue));
40 $search->subsidiary = $searchMultiSelectField;
41 
42 //perform search
43 $request = new SearchRequest();
44 $request->searchRecord = $search;
45 $searchResponse = $service->search($request);
46 
47 //repeat call for all paged results & print response
48 while(isset($searchResponse->searchResult->pageIndex) && ($searchResponse->searchResult->pageIndex <= $searchResponse->searchResult->totalPages)){
49 	print "Page: ".$searchResponse->searchResult->pageIndex."/".$searchResponse->searchResult->totalPages."</br>";
50 	if ($searchResponse->searchResult->status->isSuccess) {
51 		print "<pre>";
52 		print_r($searchResponse);
53 		print "</pre>";
54 	}
55 	$searchMoreWithIdRequest = new SearchMoreWithIdRequest();
56 	$searchMoreWithIdRequest->searchId = $searchResponse->searchResult->searchId;
57 	$searchMoreWithIdRequest->pageIndex = $searchResponse->searchResult->pageIndex+1;
58 	$searchResponse = $service->searchMoreWithId($searchMoreWithIdRequest);
59 }
60 ?>

Closing remarks

I removed a lot of the logic and processing above because it was unique to the customer, and most of that stuff is pretty mundane anyways. If you’re new to the NetSuite SuiteTalk API, and looking to perform some search queries, hopefully this will be useful.

Note: at the time of this implementation, SuiteTalk 2014.2 had just been released, but the latest PHP_Toolkit available was for version 2014.1: PHPToolkit_2014_1