Skip to content

Commit 1af5545

Browse files
cguntheriloveitaly
andauthored
Fix accessing custom field values returned in advanced search results (#480)
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. Co-authored-by: Michael Bianco <[email protected]>
1 parent 4456ce2 commit 1af5545

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

lib/netsuite/records/custom_field_list.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ def extract_custom_field(custom_field_data)
110110
attrs = custom_field_data.clone
111111
type = (custom_field_data[:"@xsi:type"] || custom_field_data[:type])
112112

113-
if type == "platformCore:SelectCustomFieldRef"
113+
case type
114+
when "platformCore:SelectCustomFieldRef", "platformCore:SearchColumnSelectCustomField"
114115
attrs[:value] = CustomRecordRef.new(custom_field_data[:value])
115-
elsif type == 'platformCore:MultiSelectCustomFieldRef'
116+
when 'platformCore:MultiSelectCustomFieldRef', 'platformCore:SearchColumnMultiSelectCustomField'
116117
# if there is only a single selection, `:value` will be hash not an array
117118
attrs[:value] = if custom_field_data[:value].is_a?(Array)
118119
custom_field_data[:value]

lib/netsuite/support/search_result.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ def initialize(response, result_class, credentials)
5252
record_list = [record_list] unless record_list.is_a?(Array)
5353

5454
record_list.each do |record|
55-
# TODO because of customFieldList we need to either make this recursive
56-
# or handle the customFieldList as a special case
57-
5855
record.each_pair do |search_group, search_data|
5956
# skip all attributes: look for :basic and all :xxx_join
6057
next if search_group.to_s.start_with?('@')
@@ -63,7 +60,25 @@ def initialize(response, result_class, credentials)
6360
# all return values are wrapped in a <SearchValue/>
6461
# extract the value from <SearchValue/> to make results easier to work with
6562

66-
if v.is_a?(Hash) && v.has_key?(:search_value)
63+
if k == :custom_field_list
64+
# Here's an example of a response
65+
66+
# <platformCommon:customFieldList>
67+
# <platformCore:customField internalId="1756" scriptId="custitem_stringfield" xsi:type="platformCore:SearchColumnStringCustomField">
68+
# <platformCore:searchValue>sample string value</platformCore:searchValue>
69+
# </platformCore:customField>
70+
# <platformCore:customField internalId="1713" scriptId="custitem_apcategoryforsales" xsi:type="platformCore:SearchColumnSelectCustomField">
71+
# <platformCore:searchValue internalId="4" typeId="464"/>
72+
# </platformCore:customField>
73+
# </platformCommon:customFieldList>
74+
75+
custom_field_list = v.fetch(:custom_field)
76+
custom_field_list = [custom_field_list] unless custom_field_list.is_a?(Array)
77+
record[search_group][k][:custom_field] = custom_field_list.map do |custom_field|
78+
custom_field[:value] = custom_field.fetch(:search_value)
79+
custom_field
80+
end
81+
elsif v.is_a?(Hash) && v.has_key?(:search_value)
6782
# Here's an example of a record ref and string response
6883

6984
# <platformCommon:entity>

spec/netsuite/actions/search_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@
171171
expect(search.results.first.internal_id).to eq('123')
172172
expect(search.results.first.external_id).to eq('456')
173173
expect(search.results.first.alt_name).to eq('A Awesome Name')
174+
expect(search.results.first.custom_field_list.custitem_stringfield.value).to eq('sample string value')
175+
expect(search.results.first.custom_field_list.custitem_apcategoryforsales.value.internal_id).to eq('4')
174176
expect(search.results.last.email).to eq('[email protected]')
175177
end
176178
end

spec/support/fixtures/search/saved_search_joined_custom_customer.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
<platformCommon:phone>
4444
<platformCore:searchValue>444-444-4444</platformCore:searchValue>
4545
</platformCommon:phone>
46+
<platformCommon:customFieldList>
47+
<platformCore:customField internalId="1756" scriptId="custitem_stringfield" xsi:type="platformCore:SearchColumnStringCustomField">
48+
<platformCore:searchValue>sample string value</platformCore:searchValue>
49+
</platformCore:customField>
50+
<platformCore:customField internalId="1713" scriptId="custitem_apcategoryforsales" xsi:type="platformCore:SearchColumnSelectCustomField">
51+
<platformCore:searchValue internalId="4" typeId="464"/>
52+
</platformCore:customField>
53+
</platformCommon:customFieldList>
4654
</listRel:basic>
4755
<listRel:contactJoin xmlns:platformCommon="urn:common_2012_1.platform.webservices.netsuite.com">
4856
<platformCommon:email>

0 commit comments

Comments
 (0)