From 5e7acb1e6da89bd8943cae7aa73b1e32b70eb6f8 Mon Sep 17 00:00:00 2001 From: Chris Gunther Date: Tue, 1 Jun 2021 10:37:55 -0400 Subject: [PATCH] Fix accessing custom field values returned in advanced search results NetSuite seems to wrap values in a `searchValue` element rather than just `value`, breaking the ability to access those values the same in an advanced search as compared to either a basic search or simple `get`. Previously, you'd access the value via `my_record.custom_field_list.my_custom_field.attributes.fetch(:search_value)`, and if it was a `CustomRecordRef`, you'd get a raw hash back, not an instance of `CustomRecordRef`. Now, you'd access the value via `my_record.custom_field_list.my_custom_field.value`, so it's consistent across `get`, basic, and advanced searches, plus you'd get an instance of `CustomRecordRef`, where appropriate. --- lib/netsuite/records/custom_field_list.rb | 5 ++-- lib/netsuite/support/search_result.rb | 23 +++++++++++++++---- spec/netsuite/actions/search_spec.rb | 2 ++ .../saved_search_joined_custom_customer.xml | 8 +++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/netsuite/records/custom_field_list.rb b/lib/netsuite/records/custom_field_list.rb index 81a7cc555..d9c21decf 100644 --- a/lib/netsuite/records/custom_field_list.rb +++ b/lib/netsuite/records/custom_field_list.rb @@ -110,9 +110,10 @@ def extract_custom_field(custom_field_data) attrs = custom_field_data.clone type = (custom_field_data[:"@xsi:type"] || custom_field_data[:type]) - if type == "platformCore:SelectCustomFieldRef" + case type + when "platformCore:SelectCustomFieldRef", "platformCore:SearchColumnSelectCustomField" attrs[:value] = CustomRecordRef.new(custom_field_data[:value]) - elsif type == 'platformCore:MultiSelectCustomFieldRef' + when 'platformCore:MultiSelectCustomFieldRef', 'platformCore:SearchColumnMultiSelectCustomField' attrs[:value] = custom_field_data[:value].map do |entry| CustomRecordRef.new(entry) end diff --git a/lib/netsuite/support/search_result.rb b/lib/netsuite/support/search_result.rb index bd7071a18..2cd936c5f 100644 --- a/lib/netsuite/support/search_result.rb +++ b/lib/netsuite/support/search_result.rb @@ -52,9 +52,6 @@ def initialize(response, result_class, credentials) record_list = [record_list] unless record_list.is_a?(Array) record_list.each do |record| - # TODO because of customFieldList we need to either make this recursive - # or handle the customFieldList as a special case - record.each_pair do |search_group, search_data| # skip all attributes: look for :basic and all :xxx_join next if search_group.to_s.start_with?('@') @@ -63,7 +60,25 @@ def initialize(response, result_class, credentials) # all return values are wrapped in a # extract the value from to make results easier to work with - if v.is_a?(Hash) && v.has_key?(:search_value) + if k == :custom_field_list + # Here's an example of a response + + # + # + # sample string value + # + # + # + # + # + + custom_field_list = v.fetch(:custom_field) + custom_field_list = [custom_field_list] unless custom_field_list.is_a?(Array) + record[search_group][k][:custom_field] = custom_field_list.map do |custom_field| + custom_field[:value] = custom_field.fetch(:search_value) + custom_field + end + elsif v.is_a?(Hash) && v.has_key?(:search_value) # Here's an example of a record ref and string response # diff --git a/spec/netsuite/actions/search_spec.rb b/spec/netsuite/actions/search_spec.rb index 2f0abbec4..b6eff64bc 100644 --- a/spec/netsuite/actions/search_spec.rb +++ b/spec/netsuite/actions/search_spec.rb @@ -171,6 +171,8 @@ expect(search.results.first.internal_id).to eq('123') expect(search.results.first.external_id).to eq('456') expect(search.results.first.alt_name).to eq('A Awesome Name') + expect(search.results.first.custom_field_list.custitem_stringfield.value).to eq('sample string value') + expect(search.results.first.custom_field_list.custitem_apcategoryforsales.value.internal_id).to eq('4') expect(search.results.last.email).to eq('alessawesome@gmail.com') end end diff --git a/spec/support/fixtures/search/saved_search_joined_custom_customer.xml b/spec/support/fixtures/search/saved_search_joined_custom_customer.xml index 68b3fa4d5..22d47550d 100644 --- a/spec/support/fixtures/search/saved_search_joined_custom_customer.xml +++ b/spec/support/fixtures/search/saved_search_joined_custom_customer.xml @@ -43,6 +43,14 @@ 444-444-4444 + + + sample string value + + + + +