NetSuite SuiteTalk Custom Record Search
October 13, 2014
In a continued effort to release more examples of NetSuite SuiteTalk examples into the wild (PHPToolkit: 2014_1), I’ve attached another example from a recent project.
In the example below, we’re querying a custom record type with internalID 215.
Code
netsuite.php:
1 <?php
2 require_once 'path/to/PHPToolkit_2014_1/PHPToolkit/NetSuiteService.php';
3
4 $service = new NetSuiteService();
5
6 /*******************************
7 * Settings
8 *******************************/
9 $days_to_update = 2; //all records updated after this number of days in the past will be updated
10 $results_per_page = 50;
11
12 //get correct data center url
13 $params = new GetDataCenterUrlsRequest();
14 $params->account = "XXXXXXX";
15 $response = $service->getDataCenterUrls($params);
16 $nshost = $response->getDataCenterUrlsResult->dataCenterUrls->webservicesDomain;
17
18 //date search: creates a date record referencing all items after $days_to_update ago.
19 //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
20 $searchDateField = new SearchDateField();
21 $searchDateField->operator = "after";
22 $searchDateField->searchValue = date('Y-m-d\TH:i:s.000\Z', strtotime('-'.$days_to_update.' days'));
23
24
25 /**********************************************
26 * Update all locator records
27 **********************************************/
28
29 $service = new NetSuiteService();
30 $service->setSearchPreferences(false, $results_per_page);
31
32 $CustomRecordSearch = new CustomRecordSearch();
33 $CustomRecordSearch->basic = new CustomRecordSearchBasic();
34 $CustomRecordSearch->basic->recType = new RecordRef();
35 $CustomRecordSearch->basic->recType->internalId = '215';
36 $CustomRecordSearch->basic->recType->type = 'customRecord';
37 $CustomRecordSearch->basic->lastModified = $searchDateField;
38
39 $searchRequest = new SearchRequest();
40 $searchRequest->searchRecord = $CustomRecordSearch;
41
42 $searchResponse = $service->search($searchRequest);
43
44 //repeat call for all paged results
45 while(isset($searchResponse->searchResult->pageIndex) && ($searchResponse->searchResult->pageIndex <= $searchResponse->searchResult->totalPages)){
46 print "Page: ".$searchResponse->searchResult->pageIndex."/".$searchResponse->searchResult->totalPages."</br>";
47 if ($searchResponse->searchResult->status->isSuccess) {
48 locationResults($searchResponse->searchResult->recordList->record);
49 }
50 $searchMoreWithIdRequest = new SearchMoreWithIdRequest();
51 $searchMoreWithIdRequest->searchId = $searchResponse->searchResult->searchId;
52 $searchMoreWithIdRequest->pageIndex = $searchResponse->searchResult->pageIndex+1;
53 $searchResponse = $service->searchMoreWithId($searchMoreWithIdRequest);
54 }
55
56 function locationResults($searchResults){
57 foreach($searchResults as $key => $searchResult){
58 $location['locator_id'] = $searchResult->internalId;
59 $location['customer_id'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_customer")->internalId;
60 $location['address1'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_add1");
61 $location['address2'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_add2");
62 $location['address3'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_add3");
63 $location['city'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_city");
64 $location['state'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_state");
65 $location['zip'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_zip");
66 $location['country'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_country")->name;
67 $location['phone'] = findCustomRecord($searchResult->customFieldList->customField,"custrecord_si_sl_phone");
68 $location['inactive_address'] = (!empty($searchResult->isInactive))?1:0;
69 updateLocationRecord($location); //function not included, basically syncs updated records with database for our use case
70 }
71 }
72
73 function findCustomRecord($records,$scriptId){
74 foreach($records as $record){
75 if(isset($record->scriptId) && $record->scriptId == $scriptId){
76 return $record->value;
77 }
78 }
79 return false;
80 }
81
82
83 ?>
Closing remarks
For another example, check out my previous post: NetSuite SuiteTalk PHP Search Query