I have been working on Netsuite PHP Toolkit for some time. Below are some code samples and tips I found useful. Any suggestion/comments are welcome.
Tips
Netsuite Webservices have concurrent session issue. This can be handled by adding a simple loop in make call function.
In PHPToolkit functions are not enclosed in try catch blocks. It is a good practice to add them to be safe from unexpected results.
GetAll Operation can be used to fetch many type of lists in netsuite
$result = $myNSClient->getAll(‘supportCaseIssue’);
Get Operation
$RecordRef = new nsRecordRef(array('internalId' => 23, 'type' => 'customer'));
$getResponse = $myNSClient->get($RecordRef);
Search by Internal Id
$search = new nsComplexObject("ContactSearchBasic");
$search->setFields(array("internalId" => array("operator" => 'anyOf', "searchValue" => new nsRecordRef(array('internalId' => ‘24’, 'type' => 'contact')))));
$searchResponse = $myNSClient->search($search);
Clearing Fields
In update operation if you want to clear an already set field you have to use nullfield list.
Below is the code to clear the email and phone fields in any case object.
$nullFieldArray = array(); $nullFieldArray['name'] = array(); $nullFieldArray['name'][] = ‘email’; $nullFieldArray['name'][] = ‘phone’; $caseFields['nullFieldList'] = $nullFieldArray;
Join Search
Get companies attached with a contact
$searchAdvance = new nsComplexObject('CustomerSearchAdvanced');
$search = new nsComplexObject('CustomerSearch');
$searchBasic = new nsComplexObject('ContactSearchBasic');
$searchMultiSelectField = new nsComplexObject('SearchMultiSelectField');
$searchValue = new nsRecordRef(array('type' => 'contact', 'internalId' => $contactInternalId));
$searchMultiSelectField->setFields( array( 'operator'=>'anyOf','searchValue'=>$searchValue));
$searchBasic->setFields(array('internalId'=>$searchMultiSelectField));
$joinFields = array('contactJoin'=>$searchBasic);
$search->setFields($joinFields);
$searchAdvance->setFields(array('criteria'=>$search));
$searchResponse = $myNSClient->search($searchAdvance);
Advertisement

February 15, 2011 at 3:09 pm
[...] Webservices have concurrent session issue. This can be handled by adding a simple loop in… [full post] dreamxtream DreamXtream's Blog netsuiteprogramming & developmentbest practicesprogramming [...]
May 31, 2011 at 10:12 pm
Thanks for posting this. I’m trying to figure out where to get the valid types for ->getAll(). Where did you get the valid list?
June 1, 2011 at 11:05 am
WebService Supported Records ( https://system.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteFlex/WebServices/STR_SupportedRecords.html ) contains documentation for all record. You can get the detail of every record type and see if it support getAll operation or not.
June 1, 2011 at 3:48 pm
Thank you. That’s what has been missing for me.
June 22, 2011 at 11:34 pm
Maki: I writing .Net application using Netsuite WSDL. I need to search for Support Case. I was able to get that, but not the messages associated with a case. I see that there is messagesjoin functionality, but I am not sure how to use it. Does anyone have idea how to do that. Thank you.
July 21, 2011 at 5:27 am
I haven’t written any real code in about 10 years now. I am very rusty at writing, but still read many programming languages fluently. I’m finding that the netsuite documentation on their PHP api is really less than stellar at this point. Anyways, for right now I have the php samples, I can add a customer. What I’m wanting do find is the response to the myNSclient->add($customer).
Basically, i’m wanting build a customer portal that accesses bits and pieces of information from several different systems and assembles it nicely in a web UI where the end user has some limited actions that they can perform. The ability to add, and update customer data in netsuite (more generically ERP) is a cornerstone in this development effort. Since the ERP data is cornerstone, I want to use the ERP customer ID’s as keys in my own local system.
Any insight is appreciated, I’ll keep googling, and working with the examples in the meantime.
July 21, 2011 at 12:24 pm
@stagg
Here is the code to get the customer id
$addResponse = $myNSClient->add($customer);
if ($addResponse->isSuccess)
$id = $addResponse->recordRef->nsComplexObject_fields['internalId'];
July 28, 2011 at 2:34 am
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…
July 28, 2011 at 12:06 pm
@shelly warren
hope code below is what you want
$customerSearch = new nsComplexObject(“CustomerSearchBasic”);
$customerSearchFields['entityStatus'] = array(“operator” => “anyOf”,
“searchValue” => array(new nsRecordRef(array(‘type’ => ‘customerStatus’,
‘internalId’ => 17))));
$customerSearch->setFields($customerSearchFields);
$myNSClient->search($customerSearch);
July 28, 2011 at 10:23 pm
Would you happen to have a cashsale sample that you could share?
Below is my little r&d sample script that I’ve been trying to record a cashsale with.
/* statically setting some post variables while I learn how to do this */
$entity = 1376; // post variable
$itemIntId = 136; // post variable 136 is a misc item.
$quantity = 1; // post variable
$price = 10.00; // post variable, a custom value for the misc item
$email = ‘ima@user.com’; // maybe post variable, maybe obtained by some other means
$paymentmethod = 1; // 1 is for cash method. setup->accounting->accounting lists
global $myNSclient;
global $account;
$cashSaleFields = array(
‘entity’ => new nsRecordRef(array(‘internalId’ => $entity)),
‘paymentmethod’ => $paymentmethod,
‘chargeit’ => false,
‘undepfunds’ => true,
‘itemList’ => array(
‘item’ => array(
‘item’ => array(‘internalId’ => $itemIntId),
‘quantity’ => $quantity,
‘price’ => $price
)
)
);
$cashSale = new nsComplexObject(“CashSale”, $cashSaleFields);
print_r($cashSale->getFields());
$addResponse = $myNSclient->add($cashSale);
if ( !$addResponse->isSuccess ) {
$success = false;
} else {
$success = true;
}
July 29, 2011 at 3:51 am
I figured it out. For anyone else in the world searching my code above is basically correct. I needed to build the paymentMethod differently than just supplying the int to the cashSaleFields
‘paymentMethod’ => array(‘internalId’ =>$paymentMethod),
July 30, 2011 at 12:24 am
dreamxtream
you are awesome!
Could I ask for an example to condition for the date created – this seems to be another pain point I can seem to get correct?
Thanks you so very much, you saved me HOURS!!
August 1, 2011 at 9:07 pm
This post is terrific, your answers are spot on – thank you!
I need to create a search that conditions a set of customer records based on the jobtype value of a project/job:
$customerSearch = new nsComplexObject(“CustomerSearchBasic”);
$jobSearch = new nsComplexObject(“JobSearchBasic”);
$jobSearch->setFields(array(‘searchValue’ => array(new nsListOrRecordRef(array(‘internalId’ => 1)),
new nsListOrRecordRef(array(‘internalId’ => 2)),
new nsListOrRecordRef(array(‘internalId’ => 3))
),
‘internalId’ => ‘jobType’,
‘operator’ => ‘anyOf’));
$searchItemFields = array (‘jobType’ => $jobSearch);
$customerSearch->setFields($searchItemFields);
This is not working, can anyone tell me what I missed or how to get the joined search to work?
Thanks in advance!
August 1, 2011 at 9:32 pm
Here is the second try – using your example, I feel like I am so close and yet… so far…
$searchAdvance = new nsComplexObject(‘CustomerSearchAdvanced’);
$search = new nsComplexObject(‘CustomerSearch’);
$searchBasic = new nsComplexObject(‘JobSearchBasic’);
$searchMultiSelectField = new nsComplexObject(‘SearchMultiSelectField’);
//$searchValue = new nsRecordRef(array(‘type’ => ‘jobType’, ‘internalId’ => 1));
$searchMultiSelectField->setFields( array( ‘operator’=>’noneOf’,
‘searchValue’=>new nsRecordRef(array(‘type’ => ‘jobType’, ‘internalId’ => 1)),
new nsRecordRef(array(‘type’ => ‘jobType’, ‘internalId’ => 2)),
new nsRecordRef(array(‘type’ => ‘jobType’, ‘internalId’ => 3))
));
$searchBasic->setFields(array(‘jobType’=>$searchMultiSelectField));
$joinFields = array(‘jobJoin’=>$searchBasic);
$search->setFields($joinFields);
$searchAdvance->setFields(array(‘criteria’=>$search));
$searchResponse = $myNSclient->search($searchAdvance);
August 2, 2011 at 8:16 am
@shelly warren
What error NetSuite/PHP is throwing for your code?
August 2, 2011 at 6:00 pm
@dreamxtream
I am seeing no error – the result set is not being effected as I expect. Does this mean the code above looks accurate to you? Again – thank you for your time!
August 2, 2011 at 9:42 pm
Found it: It is “is”. Why wont NetSuite publish good docs!
October 10, 2011 at 6:21 pm
Hi there,
Can you provide an example with filter on dates like dateCreated ?
Thanks so much !!!
October 12, 2011 at 12:15 am
@alex
You can use following operators for dates
after, before, isempty, notafter, notbefore, noton, notonorafter, notonorbefore, notwithin, on, onorafter, onorbefore, within
a sample call is given below
new nlobjSearchFilter(‘trandate’,null,’notafter’,new Date())
November 15, 2011 at 12:50 pm
@dreamxtream
Thanks for your help !
I’m stuck with another call : GetDeletedFilter
This is what I’ve done so far :
$searchhandle = new nsComplexObject(“GetDeletedFilter”);
$formated_date = “2011-11-01T00:16:17Z”;
$filter["deletedDate"] = array(“operator” => ‘onOrAfter’, “searchValue” => $formated_date);
$filter["type"] = array(“searchValue” => “salesOrder”);
$searchhandle->setFields($filter);
$results = $myNSclient->search($searchhandle);
It doesn’t seem to work ? cheers
November 26, 2011 at 8:10 pm
Great post. NetSuite PHP has been a challenging and frustrating experience for me. I’ve had to work with custom record types alot which isn’t supported well in the toolkit netsuite gives you, I’ve wrapped alot of the more generic code I’ve used into a library on github https://github.com/iloveitaly/netsuite-kohana. Hope it helps someone out.
February 6, 2012 at 10:18 pm
Not sure if you are still around, but having a hell of trouble getting the following: I would like to return all products (items) associated with a category. Building a front-end web site and trying to integrate with netsuite. Using your contact example here is what I have so far…Any help is appreciated. Thanks.
$searchAdvance = new nsComplexObject('SiteCategorySearchAdvanced');
$search = new nsComplexObject('SiteCategorySearch');
$searchBasic = new nsComplexObject('SiteCategorySearchBasic');
$searchMultiSelectField = new nsComplexObject('SearchMultiSelectField');
$searchValue = new nsRecordRef(array('type' => 'siteCategory', 'internalId' => $categoryID));
$searchMultiSelectField->setFields( array( 'operator'=>'anyOf','searchValue'=>$searchValue));
$searchBasic->setFields(array('internalId'=>$searchMultiSelectField));
$joinFields = array('basic'=>$searchBasic);
$search->setFields($joinFields);
$searchAdvance->setFields(array('criteria'=>$search));
$searchResponse = $myNSClient->search($searchAdvance);
var_dump($searchResponse);
exit;
Gives me the following error:
Fatal error: Call to a member function search() on a non-object in C:\inetpub\wwwroot\partsforscooters.com\category2.php on line 48
February 7, 2012 at 11:27 am
@alberto
Have you created the $NSClient object using this code
$myNSClient = new nsClient( nsHost::live );
$myNSClient->setPassport($email, $password, $account,$role);
February 21, 2012 at 9:56 pm
Hello dreamxtream,
Thank you for your examples and apparent willingness to help people out on your blog. I have a problem I’m hoping you could help me with. I am working on a script that pulls some information in from external systems and updates a custom field in customer records for billing purposes. My current problem is that I’m trying to find the next billing date for the customer so I can reset the counts that are being pulled in from the external systems. Each customer has a memorized transaction that auto-bills them at monthly intervals. I can pull up the memorized transactions in netsuite and get the next date, but I need to do this programmatically. There doesn’t appear to be a MemorizedTransactionSearch[Basic|Advanced] search type, but there is one for TransactionSearch[Basic|Advanced]. To start out I’ve been just trying to get all transactions associated with a customer and figure out from there how to filter down to just the memorized ones and then pull out the nextdate field to get what I want. I’ve used your contactJoin example as a starting point, but I’m not able to get an accurate list of transactions. Do you have any insight that could help me here? Below is an example of what I’ve tried. I’ve tinkered with most of the variables but no luck.
$customerInternalId = XXXXXXX;
$myNSclient->setSearchPreferences(false, 10);
$search = new nsComplexObject(“TransactionSearchBasic”);
$searchAdvance = new nsComplexObject(‘TransactionSearchAdvanced’);
$search = new nsComplexObject(‘TransactionSearch’);
$searchBasic = new nsComplexObject(‘CustomerSearchBasic’);
$searchMultiSelectField = new nsComplexObject(‘SearchMultiSelectField’);
$searchValue = new nsRecordRef(array(‘type’ => ‘customer’, ‘internalId’ => $customerInternalId));
$searchMultiSelectField->setFields( array( ‘operator’=>’anyOf’,'searchValue’=>$searchValue));
$searchBasic->setFields(array(‘internalId’=>$searchMultiSelectField));
$joinFields = array(‘customerJoin’=>$searchBasic);
$search->setFields($joinFields);
$searchAdvance->setFields(array(‘criteria’=>$search));
$searchResponse = $myNSclient->search($searchAdvance);
var_dump($searchResponse);
This gets me the following output, but if I look in netsuite there are several transactions for that customer, including memorized ones.
# php getTrans.php
PHP Notice: Undefined property: stdClass::$pageSize in /root/develop/netsuite/PHPtoolkit.php on line 425
PHP Notice: Undefined property: stdClass::$pageIndex in /root/develop/netsuite/PHPtoolkit.php on line 427
PHP Notice: Undefined property: stdClass::$record in /root/develop/netsuite/PHPtoolkit.php on line 432
PHP Notice: Undefined property: stdClass::$record in /root/develop/netsuite/PHPtoolkit.php on line 443
object(nsSearchResponse)#17 (9) {
["isSuccess"]=>
bool(true)
["statusDetail"]=>
NULL
["totalRecords"]=>
int(0)
["pageSize"]=>
NULL
["totalPages"]=>
int(0)
["pageIndex"]=>
NULL
["searchId"]=>
string(60) “XXXXXXXXXX”
["recordList"]=>
NULL
["searchRowList"]=>
NULL
}
February 22, 2012 at 6:25 pm
@Aubrey W
Did you make sure the customerId you are using is correct. That is, it should be a correct internal id of an existing customer which does have some transactions associated.
February 22, 2012 at 6:35 pm
@Aubrey W
Or may be you can try this one:
$search = new nsComplexObject(“TransactionSearchBasic”);
$fields =array(“entity” => array(“operator” => “anyOf”, “searchValue” => new nsRecordRef(array(‘internalId’ => $customerInternalId, ‘type’ =>’customer’))));
$search->setFields($fields);
$searchResponse = $myNSClient->search($search);
var_dump($searchResponse);
February 22, 2012 at 7:44 pm
I’m sure about the customerId. I tried your code as well and get the same results. However, if I go in to NetSuite GUI and do Lists > Search > Saved Search > New, choose “Transaction” for the search type, and then add a criteria field of “Customer (Main Line) Fields… > InternalId” and set it to the same thing I have in my code, I get back plenty of results. I added a Results column of “Customer (Main Line) Fields… > InternalId” and verified in the results that the internalId of the customer for the transaction matches what I expect. Additionally, I can go to the System Notes for the customer I’m looking at and see the internal id is what I expect. I’m baffled.