Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds AddList to Actions #406

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/netsuite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Support

module Actions
autoload :Add, 'netsuite/actions/add'
autoload :AddList, 'netsuite/actions/add_list'
autoload :Delete, 'netsuite/actions/delete'
autoload :DeleteList, 'netsuite/actions/delete_list'
autoload :Get, 'netsuite/actions/get'
Expand Down
118 changes: 118 additions & 0 deletions lib/netsuite/actions/add_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# https://system.eu1.netsuite.com/app/help/helpcenter.nl?fid=section_N3481360.html
module NetSuite
module Actions
class AddList
include Support::Requests

def initialize(*objects)
@objects = objects
end

private

def request(credentials={})
NetSuite::Configuration.connection(
{ element_form_default: :unqualified }, credentials
).call(:add_list, message: request_body)
end

# <soap:Body>
# <addList>
# <record xsi:type="listRel:Customer" externalId="ext1">
# <listRel:entityId>Shutter Fly</listRel:entityId>
# <listRel:companyName>Shutter Fly, Inc</listRel:companyName>
# </record>
# <record xsi:type="listRel:Customer" externalId="ext2">
# <listRel:entityId>Target</listRel:entityId>
# <listRel:companyName>Target</listRel:companyName>
# </record>
# </addList>
# </soap:Body>
def request_body
attrs = @objects.map do |o|
hash = o.to_record.merge({
'@xsi:type' => o.record_type
})

if o.respond_to?(:external_id) && o.external_id
hash['@externalId'] = o.external_id
end

hash
end

{ 'record' => attrs }
end

def response_hash
@response_hash ||= Array[@response.body[:add_list_response][:write_response_list][:write_response]].flatten
end

def response_body
@response_body ||= response_hash.map { |h| h[:base_ref] }
end

def response_errors
if response_hash.any? { |h| h[:status] && h[:status][:status_detail] }
@response_errors ||= errors
end
end

def errors
errors = response_hash.select { |h| h[:status] }.each_with_index.map do |obj, index|
error_obj = obj[:status][:status_detail]
next if error_obj.nil?
error_obj = [error_obj] if error_obj.class == Hash
errors = error_obj.map do |error|
NetSuite::Error.new(error)
end

external_id =
(obj[:base_ref] && obj[:base_ref][:@external_id]) ||
(@objects[index].respond_to?(:external_id) && @objects[index].external_id)
[external_id, errors]
end
Hash[errors]
end

def success?
@success ||= response_hash.all? { |h| h[:status][:@is_success] == 'true' }
end

module Support

def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def add_list(records, credentials = {})
netsuite_records = records.map do |r|
if r.is_a?(self)
r
else
new(r)
end
end

response = NetSuite::Actions::AddList.call(netsuite_records, credentials)

if response.success?
response.body.map do |attr|
record = netsuite_records.find do |r|
r.external_id == attr[:@external_id]
end

record.instance_variable_set('@internal_id', attr[:@internal_id])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aom what happens here if you don't set an externalID on the record you are running through addList? record would be nil and throw an exception, right?

Are the results returned by addList just a internal and external ID, or is the whole record returned?

Also feels weird to use instance_variable_set here, but without modifying every record in the gem there isn't a great alternative right now. The only thing I'd like to explore here is if we can use the results from addList to generate a new list of records to return. This would eliminate the find usage and instance_variable_set.

What do you think? I haven't worked with addList so I could be wrong here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iloveitaly You might confusing AddList with UpsertList. You don't need to define externalId for AddList records because they are always added to NetSuite unless other validation errors occur. With UpsertList you'll need to define a unique externalId for new records or internalId for existing records.

Yes, unfortunately AddList returns just the reference to NetSuite Record with internalId and externalId. No luck there.

But you did point out a bug!

I'll include another test case without externalId. Because instance_variable_set will throw an exception if it is called for nil record. As I said above, the externalId is not mandatory for AddList records (I use it anyway on my queries so I didn't fall into this trap).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a safe guard in case add_list is being called without external_id.

end

netsuite_records
else
false
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/netsuite/records/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Account
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :add, :update, :delete, :search, :upsert
actions :get, :get_list, :add, :add_list, :update, :delete, :search, :upsert

fields :acct_name, :acct_number, :acct_type, :cash_flow_rate, :cur_doc_num, :description, :eliminate, :exchange_rate,
:general_rate, :include_children, :inventory, :is_inactive, :opening_balance, :revalue, :tran_date, :balance
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/accounting_period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AccountingPeriod
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :add, :delete, :upsert, :search
actions :get, :get_list, :add, :add_list, :delete, :upsert, :search

fields :allow_non_gl_changes, :end_date, :is_adjust, :is_quarter, :is_year, :period_name, :start_date

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/assembly_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AssemblyBuild
include Support::Fields
include Namespaces::TranInvt

actions :get, :add, :initialize, :delete, :update, :upsert, :upsert_list,
actions :get, :add, :add_list, :initialize, :delete, :update, :upsert, :upsert_list,
:search

fields :bin_numbers, :buildable, :created_date, :expiration_date,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/assembly_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AssemblyItem
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :get_select_value, :add, :delete, :update, :upsert, :upsert_list, :search
actions :get, :get_list, :get_select_value, :add, :add_list, :delete, :update, :upsert, :upsert_list, :search

fields :auto_lead_time, :auto_preferred_stock_level, :auto_reorder_point, :available_to_partners, :average_cost, :build_entire_assembly,
:copy_description, :cost, :cost_estimate, :cost_estimate_type, :cost_estimate_units, :cost_units, :costing_method,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/assembly_unbuild.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AssemblyUnbuild
include Support::Fields
include Namespaces::TranInvt

actions :get, :add, :initialize, :delete, :update, :upsert, :upsert_list,
actions :get, :add, :add_list, :initialize, :delete, :update, :upsert, :upsert_list,
:search

fields :bin_numbers, :built, :created_date, :expiration_date,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/billing_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BillingSchedule
include Support::Actions
include Namespaces::ListAcct

actions :get, :add, :delete, :upsert, :search
actions :get, :add, :add_list, :delete, :upsert, :search

fields :bill_for_actuals, :day_period, :frequency, :in_arrears, :initial_amount, :is_inactive,
:is_public, :month_dom, :month_dow, :month_dowim, :month_mode, :name,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Bin
attr_reader :internal_id
attr_accessor :external_id

actions :get, :add, :delete, :search, :update, :upsert
actions :get, :add, :add_list, :delete, :search, :update, :upsert

fields :bin_number, :is_inactive, :location, :memo

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/bin_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BinTransfer
include Support::Actions
include Namespaces::TranInvt

actions :get, :get_list, :add, :initialize, :delete, :update, :upsert, :search
actions :get, :get_list, :add, :add_list, :initialize, :delete, :update, :upsert, :search

fields :created_date, :last_modified_date, :memo, :subsidiary, :tran_date, :tran_id

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/cash_refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CashRefund
include Support::Actions
include Namespaces::TranCust

actions :get, :add, :initialize, :delete, :update, :upsert, :search
actions :get, :add, :add_list, :initialize, :delete, :update, :upsert, :search

fields :alt_handling_cost,
:alt_shipping_cost,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/cash_sale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CashSale
include Support::Actions
include Namespaces::TranSales

actions :get, :add, :initialize, :delete, :update, :upsert, :search
actions :get, :add, :add_list, :initialize, :delete, :update, :upsert, :search

fields :alt_handling_cost, :alt_shipping_cost, :auth_code, :bill_address, :cc_approved, :cc_expire_date, :cc_is_purchase_card_bin,
:cc_name, :cc_number, :cc_process_as_purchas_card, :cc_security_code, :cc_street, :cc_zip_code, :charge_it, :contrib_pct, :created_date,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Contact
include Support::Actions
include Namespaces::ListRel

actions :get, :get_list, :add, :delete, :delete_list, :search, :update, :upsert
actions :get, :get_list, :add, :add_list, :delete, :delete_list, :search, :update, :upsert

fields :salutation, :first_name, :middle_name, :last_name, :title, :phone, :fax, :email, :default_address,
:entity_id, :phonetic_name, :alt_email, :office_phone, :home_phone, :mobile_phone, :supervisor_phone,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/credit_memo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CreditMemo
include Support::Actions
include Namespaces::TranCust

actions :get, :get_deleted, :get_list, :add, :initialize, :delete, :update, :upsert, :upsert_list, :search
actions :get, :get_deleted, :get_list, :add, :add_list, :initialize, :delete, :update, :upsert, :upsert_list, :search

fields :alt_handling_cost, :alt_shipping_cost, :amount_paid, :amount_remaining, :auto_apply, :balance,
:bill_address, :contrib_pct, :created_date, :currency_name, :deferred_revenue, :discount_rate, :email,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Currency

# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/schema/record/currency.html

actions :get, :get_list, :get_all, :add, :update, :upsert, :upsert_list, :delete
actions :get, :get_list, :get_all, :add, :add_list, :update, :upsert, :upsert_list, :delete

fields :name, :symbol, :is_base_currency, :is_inactive, :override_currency_format, :display_symbol, :symbol_placement,
:locale, :formatSample, :exchangeRate, :fx_rate_update_timezone, :incl_in_fx_rate_updates, :currency_precision
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/custom_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CustomList
include Support::Actions
include Namespaces::SetupCustom

actions :get, :update, :get_list, :add, :delete, :search, :upsert
actions :get, :update, :get_list, :add, :add_list, :delete, :search, :upsert

# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/schema/record/customlist.html
fields :description, :name, :is_ordered, :script_id, :convert_to_custom_record,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/custom_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomRecord
include Support::Actions
include Namespaces::SetupCustom

actions :get, :update, :get_list, :add, :delete, :search, :upsert
actions :get, :update, :get_list, :add, :add_list, :delete, :search, :upsert

fields :allow_attachments, :allow_inline_editing, :allow_numbering_override, :allow_quick_search, :alt_name, :auto_name,
:created, :custom_record_id, :description, :disclaimer, :enabl_email_merge, :enable_numbering, :include_name,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/custom_record_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CustomRecordType
include Support::RecordRefs
include Support::Actions

actions :get, :get_list, :add, :delete, :upsert
actions :get, :get_list, :add, :add_list, :delete, :upsert

fields :allow_attachments, :allow_inline_editing, :allow_numbering_override, :allow_quick_search, :description,
:disclaimer, :enable_mail_merge, :enable_numbering, :include_name, :is_available_offline, :is_inactive,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Customer
include Support::Actions
include Namespaces::ListRel

actions :get, :get_list, :add, :update, :upsert, :upsert_list, :delete, :delete_list, :search
actions :get, :get_list, :add, :add_list, :update, :upsert, :upsert_list, :delete, :delete_list, :search

# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/schema/record/customer.html

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CustomerCategory
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :get_all, :add, :update, :delete, :search
actions :get, :get_list, :get_all, :add, :add_list, :update, :delete, :search

fields :name, :is_inactive

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer_deposit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomerDeposit
include Support::Records
include Namespaces::TranCust

actions :get, :get_list, :initialize, :add, :delete, :update, :upsert, :search
actions :get, :get_list, :initialize, :add, :add_list, :delete, :update, :upsert, :search

fields :created_date, :last_modified_date, :status, :payment, :tran_date, :exchange_rate, :undep_funds, :memo,
:check_num, :klass, :currency_name, :is_recurring_payment, :tran_id, :auth_code,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomerPayment
include Support::Actions
include Namespaces::TranCust

actions :get, :get_list, :initialize, :add, :delete, :update, :upsert, :search
actions :get, :get_list, :initialize, :add, :add_list, :delete, :update, :upsert, :search

fields :auth_code, :auto_apply, :cc_approved, :cc_avs_street_match, :cc_avs_zip_match,
:cc_expire_date, :cc_name, :cc_number, :cc_security_code, :cc_security_code_match, :cc_street, :cc_zip_code,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer_refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomerRefund
include Support::Actions
include Namespaces::TranCust

actions :get, :get_list, :initialize, :add, :delete, :update, :upsert, :search
actions :get, :get_list, :initialize, :add, :add_list, :delete, :update, :upsert, :search

fields :address, :cc_approved, :cc_expire_date, :cc_name, :cc_number, :cc_street, :cc_zip_code, :charge_it,
:created_date, :currency_name, :debit_card_issue_no, :exchange_rate, :last_modified_date, :memo, :pn_ref_num, :status,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomerStatus
include Support::Actions
include Namespaces::ListRel

actions :get, :add, :delete, :search, :update, :upsert
actions :get, :add, :add_list, :delete, :search, :update, :upsert

fields :description, :include_in_lead_reports, :is_inactive, :name,
:probability, :stage
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Department
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :get_select_value, :add, :delete, :upsert,
actions :get, :get_list, :get_select_value, :add, :add_list, :delete, :upsert,
:search, :update

fields :name, :is_inactive
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/deposit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Deposit
include Support::Actions
include Namespaces::TranBank

actions :get, :get_list, :add, :delete, :upsert, :update, :search
actions :get, :get_list, :add, :add_list, :delete, :upsert, :update, :search

fields :created_date, :last_modified_date, :currency_name, :exchange_rate, :tran_id, :total, :tran_date, :memo, :to_be_printed

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/deposit_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DepositApplication
include Support::Actions
include Namespaces::TranCust

actions :get, :get_list, :initialize, :add, :delete, :update, :upsert, :search
actions :get, :get_list, :initialize, :add, :add_list, :delete, :update, :upsert, :search

fields :applied,
:created_date,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/description_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SubtotalItem
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :add, :delete, :search, :update, :upsert
actions :get, :get_list, :add, :add_list, :delete, :search, :update, :upsert

fields :available_to_partners, :created_date, :description, :display_name, :include_children, :is_inactive, :item_id, :last_modified_date

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/discount_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DiscountItem
include Support::Actions
include Namespaces::ListAcct

actions :get, :get_list, :add, :update, :delete, :search, :upsert
actions :get, :get_list, :add, :add_list, :update, :delete, :search, :upsert

fields :available_to_partners, :created_date, :description, :display_name, :include_children, :is_inactive, :is_pre_tax,
:item_id, :last_modified_date, :non_posting, :rate, :upc_code, :vendor_name
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/employee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Employee

# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/script/record/employee.html

actions :get, :get_list, :add, :update, :upsert, :upsert_list, :delete, :search
actions :get, :get_list, :add, :add_list, :update, :upsert, :upsert_list, :delete, :search

fields :alt_name, :phone, :first_name, :last_name, :is_inactive, :email, :give_access, :send_email, :is_support_rep,
:birth_date, :hire_date, :last_review_date, :next_review_date, :title, :home_phone, :office_phone,
Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class File

# https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2014_1/schema/record/file.html

actions :get, :add, :delete, :search, :get_list
actions :get, :add, :add_list, :delete, :search, :get_list

fields :content, :description, :name, :media_type_name, :file_type, :text_file_encoding, :created_date, :last_modified_date

Expand Down
2 changes: 1 addition & 1 deletion lib/netsuite/records/gift_cert_redemption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GiftCertRedemption
include Support::Actions
include Namespaces::TranSales

actions :get, :get_list, :add, :initialize, :delete, :update, :upsert, :search
actions :get, :get_list, :add, :add_list, :initialize, :delete, :update, :upsert, :search

fields :auth_code_amt_remaining, :auth_code_applied, :gift_cert_available

Expand Down
Loading