Netsuite PHP Toolkit and Custom Fields

Figuring out how to set custom fields via Netsuite Webservices can be a bit difficult. Here is a quick example of how it can be done: (I am using an old version of the toolkit, but I believe the example still works on new versions)

global $myNSclient;
$listOrRecordRef = new nsComplexObject('ListOrRecordRef');
$listOrRecordRef->setFields(array(
    'internalId' => 34, 'typeId' => 1
));
$boolCustFieldRef = new nsComplexObject('BooleanCustomFieldRef');
$boolCustFieldRef->setFields(array(
    "internalId" => "custentity19",
    "value" => true
));
$selectCustFieldRef = new nsComplexObject("SelectCustomFieldRef");
$selectCustFieldRef->setFields(array(
    "internalId" => 'custentity4',
    'value' => $listOrRecordRef
));
$customFieldList = new nsComplexObject("CustomFieldList");
$customFieldList->setFields(array(
    "customField" => array($selectCustFieldRef, $boolCustFieldRef)
));
$customerSetFieldsArray = array(
    "customFieldList" => $customFieldList,
);
$customer = new nsComplexObject("Customer");
$customer>setFields($customerSetFieldsArray);
$writeResponse = $myNSclient>update($customer);

A couple notes:

  1. internalId is NOT an integer.  Notice the custentity19 value.
  2. The structure can vary greatly depending on what type of custom field you are dealing with.  When in doubt, var_dump an existing object you loaded.
This entry was posted in Netsuite PHPToolkit and tagged , , , , , . Bookmark the permalink.

18 Responses to Netsuite PHP Toolkit and Custom Fields

  1. Andrew says:

    Just wanted to thank you for the Netsuite code snippets. I’ve been working with their PHP tool kit for a few months now, and it’s nice to have someone else’s blood, sweat, and tears to learn from.

  2. Nirav says:

    I am using the netsuite phptoolkit (version 2011_1r1).
    I have tried to add the customfield ( Free -form text custom field) in insert new sales order. But it is not inserting the data.
    Following is the code:
    $OrderNumber=’22221′;

    $boolCustFieldRef = new nsComplexObject(‘StringCustomFieldRef’);
    $boolCustFieldRef->setFields(array(
    “internalId” => “custbody_webordernumber”,
    “value” => $OrderNumber
    ));
    $customFieldList = new nsComplexObject(“CustomFieldList”);
    $customFieldList->setFields(array(
    “customField” => array($boolCustFieldRef)
    ));
    $shipingMethod=7;
    //’CustomFieldList’ => $customFieldList,
    //$myNSclient->setPassport($email, $password, $account, “14”);
    // Code for insert the order in netsuite
    $salesOrderFields = array(

    ‘entity’ => new nsRecordRef(array(‘internalId’ => $entity)),
    //’paymentMethod’ => array(‘internalId’ =>$paymentMethod),
    ‘ccNumber’ => $ccNumber,
    ‘ccExpireDate’ => date(“c”, mktime(0,0,0,$month,1,$year)),
    ‘ccName’ => $ccName,
    ‘billAddress’ =>$BillingAddress,
    ‘shipAddress’ =>$BillingAddress,
    ‘terms’ => array(‘internalId’ =>4),
    ‘CustomFieldList’ => $customFieldList,
    ‘shipMethod’=>array(‘internalId’ =>$shipingMethod),
    ‘shippingCost’=>100,

    );
    $salesOrderFields[‘itemList’][‘item’][] = array(
    ‘item’ => array(‘internalId’ => $itemIntId),
    ‘quantity’ => $quantity
    );
    $salesOrderFields[‘itemList’][‘item’][] = array(
    ‘item’ => array(‘internalId’ => $itemIntId),
    ‘quantity’ => $quantity
    );
    $salesOrder = new nsComplexObject(“SalesOrder”, $salesOrderFields);

    $addResponse = $myNSclient->add($salesOrder);

    Thanks in advance.
    Nirav Patel.
    Contact : niravmscit@gmail.com

  3. vincentc says:

    Thanks a bunch for this :)

  4. Denny says:

    Dear Daniel,
    I found your site is very very useful. I check your site first for answer then i go to netsuite support haha.
    I have a bit problem here adding data for parent and child custom record.
    I manage to add data to the parent custom record, as well as the child custom record, but i cannot link them as parent and child. How to accomplish this thing ?

    I hope you got what i mean, sorry for my bad english

    Thanks

  5. Erik says:

    One question for my better understanding: what is the first object used for? The ListOrRecordRef object. My guess is, that Netsuite uses that to find the customer you are updating here, right? What is the typeId parameter you are passing to the constructor? Thanks.

  6. Neil says:

    First off thanks for doing this blog! As a babe-in-the-woods netsuite php toolkit newbie, I am finding your blog to be super useful and informative! When I say newb, I’m talking 48 hours.

    Thanks to you I am already inserting records via API :)

    I have an interesting issue. I have a custom field for our customer ID. For whatever reason, even though the information in this field is always unsigned INT, the person in charge wants to change the type to free form text. Naturally I’m against this (and have registered my protest loudly) but they want to do it and insist on changing the type.

    After making this change in the sandbox, I did some testing with the API, and the field is no longer visible at all to the API. Everything is the same except the type (naturally), and the Sourcing & Filtering, which has to be changed from
    Source List: Customer
    Source From: Customer ID
    to nothing, I guess (and I am really guessing based on common sense, not knowledge of Netsuite) because Customer->Customer ID is INT and you can’t source from a different type.

    Those are the only differences I can detect. I’ve gone into every configuration screen for the field and can’t find any other differences.

    I changed the field back to INT for a sanity check, set the Sourcing & Filtering back (in the sandbox of course) and it becomes visible again.

    I also made sure the field (for my test case) had data in it.

    Is this because:
    a. free form text custom fields aren’t accessible via API?
    b. the Sourcing & Filtering needs to be set to something?
    or
    c. a bug in PHP Toolkit?

    All I’m doing in my script is:
    global $myNSclient;

    $RecordRef = new nsRecordRef(array(‘internalId’=>32649, ‘type’=>’customer’));
    $getResponse = $myNSclient->get($RecordRef);
    print_r($getResponse);

    So there’s not a whole lot I can be doing wrong, with regard to fields getting filtered/excluded/missed.

    Any insight you can provide would be greatly appreciated!

    thx
    -Neil

  7. Manoj says:

    I am fetching the sales orders from the netSuite application , using the PHP toolkit :
    after the following commands , I get the list of all the orders and the items ordered.
    $searchResponse = $myNSclient->search($transactionsearch);
    $totalRecords = $searchResponse->totalRecords;

    I am iterating through the : foreach ($searchResponse->recordList as $record)
    This gives me the sales record .
    I can read all the basic data like $record->getField(‘tranDate’) etc.

    Then I am getign the list of the items Ordered by :
    $itemList=$record->getField(‘itemList’)

    Then I am iterrating through the items by :
    foreach ($items as $item)

    What is the best way to reference to the custom field named i.e. “weight” ?

    Can anyone share any example ?

    thanks

    • daniel says:

      You can’t reference custom fields by name. You will have to reference them by id. Something like “internalId” => ‘custentity4′. You can figure this id out by looking at the web interface.

    • Manoj says:

      Never mind.
      I got it working like this:

      $tranDateCriteria = new nsComplexObject(“SearchDateField”);
      $tranDateCriteria->setFields(array(‘searchValue’ => ‘2012-12-11T16:44:57.000Z’,’operator’ => ‘on’)); //

      $tranSearchBasic = new nsComplexObject(“TransactionSearchBasic”);
      $tranSearchBasic->setFields(array(“tranDate” => $tranDateCriteria));

      $tranSearch = new nsComplexObject(“TransactionSearch”);
      $tranSearch->setFields(array(“basic” => $tranSearchBasic));

      $searchFields = array(‘savedSearchId’ => ‘810’,’criteria’ => $tranSearch);
      $tranSearchAdvanced = new nsComplexObject(“TransactionSearchAdvanced”);
      $tranSearchAdvanced->setFields($searchFields);
      $searchResponse = $myNSclient->search($tranSearchAdvanced);

      • Manoj says:

        Please ignore this. I meant to post against another post. Where i wanted to get the data from the saved search, based upon the tranDate.

        Daniel,
        Yes, you are right that we can reference to the internal Id of the field. Btw , ‘weight’ is the internal Id of the field named ‘weight’.
        So I can get the value now. Thanks btw.

  8. Dave says:

    Daniel,

    Thanks for the post! A big help. What if I want to go the other direction for a custom list?

    For example, we have a custom list with options for how the customer heard about us. We modify this list from time to time. When a visitor to our site gets to our registration screen we’d like to pull in from NetSuite the list items for the custom list with id 3 (along with each list item’s id) so we can fill a drop-down box. This way we can maintain a single list in NetSuite and it will always be current in the website.

    If we can’t do this then how do I find the IDs in NetSuite for the custom lists items?

    Thanks again!

  9. Nancy says:

    Thanks so much for your very helpful examples. As a new developer using the 2012 endpoint, they’re been wonderful.
    To try and return the favor, for fellow 2102 users, here’s an test example of updating two custom fields (free-form text Transaction Body Fields) for a sales order:

    require_once ‘../PHPToolkit/NetSuiteService.php';
    $service = new NetSuiteService();

    /* create new sales order object for update */
    $soUpdate = new SalesOrder();
    $soUpdate->internalId = “fill in yours here”;

    /* to build the custom fields list start making one custom field e.g. the store order id */
    /* See Custom Field Types in the Platform Services Guide for which class to create,
    which sets the “type” of the custom field.
    Do a getRequest and check the response XML for an existing sales order to see the internalId of the custom object */
    $custfieldObj1 = new StringCustomFieldRef();
    $custfieldObj1->internalId = ‘custbodystorefrontorder';
    $custfieldObj1->value = ‘12268’;

    /* repeat for next custom field – which type of store this order came from */
    $custfieldObj2 = new StringCustomFieldRef();
    $custfieldObj2->internalId = ‘custbodystorefront';
    $custfieldObj2->value = ‘Magento';

    /* put those custom fields into the customField array of the custom fields list */
    $cflist = new CustomFieldList();
    $cflist->customField = array($custfieldObj1, $custfieldObj2);

    /* set as the sales order’s customFieldList attribute */
    $soUpdate->customFieldList = $cflist;

    $request = new UpdateRequest();
    $request->record = $soUpdate;

    $updateResponse = $service->update($request);

    if ($updateResponse->writeResponse->status->isSuccess)
    echo ‘worked';
    else
    echo ‘problems';

  10. Nancy says:

    Woops, meant 2012 endpoint.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>