Ruby on Rails

NetSuite API Integration with Ruby on Rails – Part 2 – Add Sales Order

NetSuite is a popular ERP software used by several businesses and can be easily integrated with a Rails project using a gem called ‘netsuite‘. In my previous post, we sent Get, Get All, Get Select Value, and Search requests to query data from NetSuite System. We also collected all the data and stored it in a handy csv.

Now, we’ll see how to create a Sales Order Object and send an Add request to add a new Sales Order to the NetSuite records.

Add Request

Lets start by creating an empty new Sales Order Object and sending the add request. In rails console type,

sales_order_record = NetSuite::Records::SalesOrder.new()
response = sales_order_record.add

You can check errors listed in sales_order_record.errors
The errors will be regarding missing mandatory fields. So let’s send a request with all mandatory fields, but no items in the Sales Order.

date = Date.today
sales_order_item_list = NetSuite::Records::SalesOrderItemList.new()
custom_field_list     = NetSuite::Records::CustomFieldList.new()
sales_order_record    = NetSuite::Records::SalesOrder.new(
  :entity             => NetSuite::Records::Customer.new(:internal_id => 2155),
  :currency           =>  NetSuite::Records::RecordRef.new(:internal_id => 1, :type => 'currency'),
  :order_status       => '_pendingFulfillment',
  :tran_date          => date,
  :tran_id            => "TEST_ID",
  :sales_rep          => NetSuite::Records::Employee.new(:internal_id => 338),
  :other_ref_num      => 'TEST_ORDER',
  :memo               => 'TEST_ORDER',
  :sales_effective_date => date,
  :to_be_printed      => false,
  :to_be_emailed      => true,
  :to_be_faxed        => false,
  :email              => "test@example.com",
  :ship_date          => date - 60,
  :subsidiary         => NetSuite::Records::Subsidiary.new(:internal_id => 12),
  :location           => NetSuite::Records::Location.new(:internal_id => 1),
  :terms              => NetSuite::Records::Term.new(:internal_id => 7),
  :item_list          => sales_order_item_list,
  :custom_field_list  => custom_field_list
)

response = sales_order_record.add

In the code snippet above, we’ve added blank lists for Custom fields, and Sales Order Items.

You can add items to the list using Internal IDs of existing Inventory Items. To find valid values for Inventory items, you can check out the csv with all the Netsuite data we pooled earlier.

item1 = NetSuite::Records::InventoryItem.new(:internal_id => 6009)

sales_order_item1 = NetSuite::Records::SalesOrderItem.new({
  :item     => item1,
  :quantity => 2,
  :rate     => 18.50,
  :amount   => 37.00
})

sales_order_item_list = NetSuite::Records::SalesOrderItemList.new()
sales_order_item_list.items << sales_order_item1

Similarly you can add data to a list of custom fields.
Say, you have a field for order delivery months. You can add this field to the custom_field_list in a sales order

custom_field = NetSuite::Records::CustomField.new(
  :internal_id =>129,
  :script_id   => "delivery_months",
  :type        =>"platformCore:SelectCustomFieldRef",
  :value       => NetSuite::Records::CustomRecordRef.new(
    :type_id     =>125,
    :internal_id =>9
  )
)
custom_field_list = NetSuite::Records::CustomFieldList.new()
custom_field_list.custom_fields << custom_field

Log XML Request and Response

Another requirement we had, was to store the XML requests and responses for each order placed, in a separate log file.
By default, the log output is printed in STDOUT or stored in the location defined in config file.
To change this behaviour, and create a file for each order placed, I looked into the gem that netsuite uses internally, Savon. Any request call, takes in some parameters apart from credentials. So using a custom call, and passing in parameters for the file name, we can force XML output to log to different files.

path     = File.join(Rails.root, 'public/export/order_123.log')
response = NetSuite::Configuration.connection({logger: Logger.new(path)}, {}).call(:add, :message => request_body(sales_order_record))

The gem is well written, so it is easy to create custom requests. The only issue may be the unfamiliarity with NetSuite terminology.
So, post away your queries below, and I’d be happy to oblige.

Leave a comment