Netsuite PHP ToolKit Tips and Code Samples

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

27 Responses to “Netsuite PHP ToolKit Tips and Code Samples”

  1. Netsuite PHP ToolKit Tips and Code Samples Says:

    [...] Webservices have concurrent session issue. This can be handled by adding a simple loop in… [full post] dreamxtream DreamXtream's Blog netsuiteprogramming & developmentbest practicesprogramming [...]

  2. Neil Hastings Says:

    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?

  3. dreamxtream Says:

    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.

  4. Neil Hastings Says:

    Thank you. That’s what has been missing for me.

  5. Anonymous Says:

    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.

  6. stagg Says:

    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.

  7. dreamxtream Says:

    @stagg
    Here is the code to get the customer id
    $addResponse = $myNSClient->add($customer);
    if ($addResponse->isSuccess)
    $id = $addResponse->recordRef->nsComplexObject_fields['internalId'];

  8. shelly warren Says:

    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…

  9. dreamxtream Says:

    @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);

  10. stagg Says:

    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;

    }

  11. stagg Says:

    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),

  12. dreamxtream Says:

    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!!

  13. shelly warren Says:

    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!

  14. shelly warren Says:

    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);

  15. dreamxtream Says:

    @shelly warren
    What error NetSuite/PHP is throwing for your code?

  16. shelly warren Says:

    @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!

  17. Shane Says:

    Found it: It is “is”. Why wont NetSuite publish good docs!

  18. alex Says:

    Hi there,
    Can you provide an example with filter on dates like dateCreated ?
    Thanks so much !!!

  19. dreamxtream Says:

    @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())

  20. alex Says:

    @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 :)

  21. Michael Bianco Says:

    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.

  22. alberto Says:

    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

  23. dreamxtream Says:

    @alberto

    Have you created the $NSClient object using this code
    $myNSClient = new nsClient( nsHost::live );
    $myNSClient->setPassport($email, $password, $account,$role);

  24. Aubrey W Says:

    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
    }

  25. Sharique Abdullah Says:

    @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.

  26. Sharique Abdullah Says:

    @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);

  27. Aubrey W Says:

    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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.