Netsuite Webservices and Multiple Item Sales Orders

By request, here is a post on creating a sales order record with Netsuite PHPToolkit with more than one item:

$items = array();
foreach ($mycartitems as $item) {
    $itemRef = new nsRecordRef(array('internalId'=>$item->getNetsuiteID()));
    $qty = $item->getQuantity();
    $SalesOrderItem = new nsComplexObject("SalesOrderItem");
    $salesOrderItemFieldArray = array(
         "item" => $itemRef,
         "quantity" => $qty
    );
    $SalesOrderItem->setFields($salesOrderItemFieldArray);
    $items[] = $SalesOrderItem;
}
$salesOrderItemList = new nsComplexObject("SalesOrderItemList");
$salesOrderItemList->setFields(array(
     "item" => $items
));
$salesOrderFields = array(
    "orderStatus" => "_pendingFulfillment",
    "entity" => $custRef,
    "getAuth" => true,
    "shippingCost" => $shipCost,
    "shipMethod" => $shipRef,
    "toBeEmailed" => true,
    "email" => $emailList,
    "itemList" => $salesOrderItemList
);

Its not all that intuitive, but that’s Netsuite Web-services for you…

This entry was posted in Netsuite PHPToolkit, PHP and tagged , , , , . Bookmark the permalink.

10 Responses to Netsuite Webservices and Multiple Item Sales Orders

  1. Denny says:

    Hi Daniel,
    It’s me again, i’ve been using your code above for my SO( Sales Order) implementation.
    It’s working perfectly.

    I have problem now on how to add the custom Shipping and billing address in SO.
    But i don’t want the address to be added to the customer record, i just want it to be a custom data in the particular Sales Order
    Do you have sample script to achieve this ?

    I can only find an example in SuiteScript, but cannot find how to do in SuiteTalk.

    • daniel says:

      I haven’t tried to do that before. I have always added the address to the customer. If you can’t find a way to do what you want, a work around might be adding it to the customer and then removing after the order completes.

  2. Denny says:

    Hi Daniel,
    Based on this article the item is referred by internalID
    $itemRef = new nsRecordRef(array(‘internalId’=>$item->getNetsuiteID()));

    Is there any way to refer the item by itemId ?

    I tried
    $itemRef = new nsRecordRef(array(‘itemId’=>’SKU123456′);
    but it’s not working

    Thanks

  3. nikhil says:

    can any body explain or tell the above code in java using webservices

  4. Nancy says:

    To add the custom address to a sales order, PHP Toolkit endpoint 2012:

    define(‘LF’, “\n”); /* for use in formatting custom addresses since NetSuite converts to <br> */

    /* this example has the customer address info in a db record, just pulled into $row */
    $billAddress = stripslashes($row['billingAddressee']).LF;
    if (!empty($row['billingCompany']))
    $billAddress .= stripslashes($row['billingCompany']).LF;
    $billAddress .= stripslashes($row['billingStreet1']).LF;
    if (!empty($row['billingStreet2']) and strlen($row['billingStreet2']) > 0)
    $billAddress .= stripslashes($row['billingStreet2']).LF;
    $billAddress.= stripslashes($row['billingCity']).’, ‘.$row['billingState'].’ ‘.$row['billingPostalCode'].’ ‘.$row['billingCountry'];
    $so->billAddress = $billAddress;

  5. Bhargav says:

    Please Help Me…….
    AddResponse Object
    (
    [writeResponse] => WriteResponse Object
    (
    [status] => Status Object
    (
    [statusDetail] => Array
    (
    [0] => StatusDetail Object
    (
    => INSUFFICIENT_PERMISSION
    [message] => You do not have permissions to set a value for element subtotal due to one of the following reasons: 1) The field is read-only; 2) An associated feature is disabled; 3) The field is available either when a record is created or updated, but not in both cases.
    [type] => ERROR
    )

    )

    [isSuccess] =>
    )

    [baseRef] =>
    )

    )

    include(‘NetSuiteService.php’);

    $service = new NetSuiteService();
    if($order_items->netsuitid > 0){
    $internal_Id = $order_items->netsuitid;
    $emailCustomer = $order_items->user_email;
    }
    else{
    $customer_Info = $order->get_customer_info($order->user_id);
    $customer_information = array();
    foreach($customer_Info as $customer_key => $customer_value){
    if($customer_value->meta_key == ‘first_name’){
    $customer_information['first_name'] = $customer_value->meta_value;
    }
    if($customer_value->meta_key == ‘last_name’){
    $customer_information['last_name'] = $customer_value->meta_value;
    }
    }

    $customer_information['email'] = $customer_Info->user_email;

    //Add customer into net suit integration
    $service = new NetSuiteService();

    $customer = new Customer();
    $customer->lastName = $customer_information['last_name'];
    $customer->firstName = $customer_information['first_name'];
    $customer->companyName = ‘Company Name’;
    $customer->phone = ’2222222222′;
    $customer->email = $customer_information['email'];
    $emailCustomer = $customer_information['email'];
    $request = new AddRequest();
    $request->record = $customer;

    $addResponse = $service->add($request);
    if (!$addResponse->writeResponse->status->isSuccess) {
    echo “You are already Registered with Netsuit.”;
    }
    else {
    $internal_Id = $addResponse->writeResponse->baseRef->internalId;
    $order->insert_Customer($internal_Id,$order->user_id);
    }
    //End customer into net suit integration
    }

    //Add Product Information
    /*$items = array();

    foreach ( $order_items as $item_id => $item ) {
    $itemRef = new nsRecordRef(array(‘internalId’=>$internal_Id));
    $qty = $item['qty'];
    if($item['type'] == ‘line_item’){
    $salesOrderItemFieldArray = array(
    “item” => $itemRef,
    “quantity” => $qty

    );
    }
    if($item['type'] == ‘fee’){
    $salesOrderItemFieldArray = array(
    “item” => $itemRef,
    “quantity” => $qty
    );
    }
    $SalesOrderItem->setFields($salesOrderItemFieldArray);
    $items[] = $SalesOrderItem;
    }

    $salesOrderItemList = new nsComplexObject(“SalesOrderItemList”);
    $salesOrderItemList->setFields(array(
    “item” => $items
    ));

    $salesOrderFields = array(
    “orderStatus” => $order->status,
    “entity” => ”,
    “getAuth” => true,
    “shippingCost” => $order->order_shipping,
    “shipMethod” => $order->payment_method,
    “toBeEmailed” => true,
    “email” => $emailCustomer,
    “itemList” => $salesOrderItemList
    );*/

    $so = new SalesOrder();
    //created Date
    //$so->createdDate = $order->order_date;
    //entity
    $so->entity = new RecordRef();
    $so->entity->internalId = $internal_Id;
    $so->entity->name = $order->order_custom_fields['_billing_company'][0];

    //Transaction Id
    //$so->tranId = $order->order_custom_fields['Transaction ID'][0];

    //Transaction Paid Date
    //$so->tranDate = $order->order_custom_fields['_paid_date'][0];

    //Source
    $so->source = ‘littlecrate’;

    //Created From
    $so->createdFrom = ‘littlecrate.com’;

    //Currency Name
    require_once(‘geoplugin.class.php’);
    $geoplugin = new geoPlugin();

    $geoplugin->currency = $order->order_custom_fields['_order_currency'];

    $so->currencyName = $geoplugin->countryName;
    $so->currency = $order->order_custom_fields['_order_currency'][0];

    //Discount
    $so->discountRate = $order->order_custom_fields['_order_discount'][0];

    //Tax
    $so->taxRate = $order->order_custom_fields['_order_tax'][0];

    //email
    $so->email = $order->billing_email;

    //Status
    //$so->orderStatus = $order->status;

    //Billing Address
    $so->billAddressList = array(
    ‘billFirstname’ => $order->order_custom_fields['_billing_first_name'][0],
    ‘lastname’ => $order->order_custom_fields['_billing_last_name'][0],
    ‘billAddressee’ => $order->order_custom_fields['_billing_address_1'][0],
    ‘billAddr1′ => $order->order_custom_fields['_billing_address_2'][0],
    ‘billCountry’ => $order->order_custom_fields['_billing_country'][0],
    ‘billState’ => $order->order_custom_fields['_billing_state'][0],
    ‘billZip’ => $order->order_custom_fields['_billing_postcode'][0],
    ‘billPhone’ => $order->order_custom_fields['_billing_phone'][0],
    ‘billEmail’ => $order->order_custom_fields['_billing_email'][0]);

    //Shipping Address
    $so->shipAddressList = array(
    ‘shipFirstname’ => $order->order_custom_fields['_shipping_first_name'][0],
    ‘shipLastname’ => $order->order_custom_fields['_shipping_last_name'][0],
    ‘shipAddressee’ => $order->order_custom_fields['_shipping_address_1'][0],
    ‘shipAddr1′ => $order->order_custom_fields['_shipping_address_2'][0],
    ‘shipCity’ => $order->order_custom_fields['_shipping_city'][0],
    ‘shipState’ => $order->order_custom_fields['_shipping_state'][0],
    ‘shipZip’ => $order->order_custom_fields['_shipping_postcode'][0],
    ‘shiplPhone’ => $order->order_custom_fields['_billing_phone'][0],
    ‘shipEmail’ => $order->order_custom_fields['_billing_email'][0]);

    //Ship Date
    //$so->shipDate = $order->order_custom_fields['Transaction ID'][0];

    //Shipping Method
    $so->shipMethod = $order->shipping_method;

    //Shipping Charges
    $so->shippingCost = $order->order_shipping;

    //Shipping Tax Rate
    $so->shippingTax1Rate = $order->order_shipping_tax;

    //Payment Method
    $so->paymentMethod = $order->payment_method;

    //Sub Total
    //$so->subTotal = $order->order_total;

    //Discount Total(Cart Total)
    //$so->discountTotal = $order->cart_discount;

    //Tax Total
    //$so->taxTotal = $order->order_tax;

    //Total
    //$so->total = $order->order_total;

    //Product Listing
    $arrItemsList = array();
    $i = 0;
    foreach($order_items_product as $keyProduct =>$valueProduct){
    if($valueProduct['type'] == ‘line_item’){
    //$arrItemsList[$i]['item']['internalId'] = $valueProduct['product_id'];
    //$arrItemsList[$i]['item']['externalId'] = $keyProduct;
    $arrItemsList[$i]['item']['name'] = $valueProduct['name'];
    $arrItemsList[$i]['item']['quantity'] = $valueProduct['qty'];
    $arrItemsList[$i]['item']['description'] = $valueProduct['type'];
    $arrItemsList[$i]['item']['amount'] = $valueProduct['line_total'];
    }
    if($valueProduct['type'] == ‘fee’){
    //$arrItemsList[$i]['item']['internalId'] = $valueProduct['product_id'];
    //$arrItemsList[$i]['item']['externalId'] = $keyProduct;
    $arrItemsList[$i]['item']['name'] = $valueProduct['name'];
    $arrItemsList[$i]['item']['quantity'] = $valueProduct['qty'];
    $arrItemsList[$i]['item']['description'] = $valueProduct['type'];
    $arrItemsList[$i]['item']['amount'] = $valueProduct['line_total'];
    }
    $i++;
    }

    //print_r($arrItemsList);

    $so->itemList->item = $arrItemsList;

    /*$so->itemList = new SalesOrderItemList();
    $soi = new SalesOrderItem();
    $soi->item = new RecordRef();
    $soi->item->internalId = 15;
    $soi->quantity = 3;
    $soi->price = new RecordRef();
    $soi->price->internalId = $id;
    $soi->amount = 55.3;
    $so->itemList->item = array($soi);*/

    $request = new AddRequest();
    $request->record = $so;
    //print_r($request);

    $addResponse = $service->add($request);
    print_r($addResponse);
    exit;
    if (!$addResponse->writeResponse->status->isSuccess) {
    echo “ADD ERROR”;
    } else {
    echo “ADD SUCCESS, id ” . $addResponse->writeResponse->baseRef->internalId;
    }

  6. Andrew Murphy says:

    Awesome very useful

    I now have it working!

    Do you have any idea how you can set the Unit Price of the sales order line?

    I can set the Quantity and the Total, but I can’t work out how to set the Unit Amount

    • daniel says:

      Looking at the schema, it seems like you might be able to set the ‘price’ record ref to custom under the itemList and then set the ‘rate’. However, I don’t know if that is implemented in earlier schemas, nor have I tried it, but that is where I would start poking around.

  7. Matt says:

    Here’s how to do it with the new PHPToolkit:

    $items = array();
    foreach ($cartitems as $item) {
    $product = get_product( $item['product_id'] ); //returns an instance
    // check whether this is a variable product – if so use it’s variable id
    if( $product->product_type == ‘variable’ )
    $product = get_product( $item['variation_id'] );
    $sku = $product->get_sku(); //returns the sku
    $price = $product->get_price();
    $product_qty = $item['qty'];

    $soi = new SalesOrderItem();
    $soi->item = new RecordRef();

    $soi->item->internalId = $sku;
    $soi->quantity = $product_qty;
    $soi->amount = ( $price * $product_qty );
    $items[] = $soi;
    }

    $so->itemList->item = $items;

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>