Skip to content

Commit

Permalink
Fix accessing custom field values returned in advanced search results (
Browse files Browse the repository at this point in the history
…NetSweet#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]>
  • Loading branch information
2 people authored and diegopolido committed Oct 7, 2021
1 parent fe7f9ff commit 6d0babe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/netsuite/records/custom_field_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
# if there is only a single selection, `:value` will be hash not an array
attrs[:value] = if custom_field_data[:value].is_a?(Array)
custom_field_data[:value]
Expand Down
23 changes: 19 additions & 4 deletions lib/netsuite/support/search_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?('@')
Expand All @@ -63,7 +60,25 @@ def initialize(response, result_class, credentials)
# all return values are wrapped in a <SearchValue/>
# extract the value from <SearchValue/> 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

# <platformCommon:customFieldList>
# <platformCore:customField internalId="1756" scriptId="custitem_stringfield" xsi:type="platformCore:SearchColumnStringCustomField">
# <platformCore:searchValue>sample string value</platformCore:searchValue>
# </platformCore:customField>
# <platformCore:customField internalId="1713" scriptId="custitem_apcategoryforsales" xsi:type="platformCore:SearchColumnSelectCustomField">
# <platformCore:searchValue internalId="4" typeId="464"/>
# </platformCore:customField>
# </platformCommon:customFieldList>

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

# <platformCommon:entity>
Expand Down
2 changes: 2 additions & 0 deletions spec/netsuite/actions/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]')
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<platformCommon:phone>
<platformCore:searchValue>444-444-4444</platformCore:searchValue>
</platformCommon:phone>
<platformCommon:customFieldList>
<platformCore:customField internalId="1756" scriptId="custitem_stringfield" xsi:type="platformCore:SearchColumnStringCustomField">
<platformCore:searchValue>sample string value</platformCore:searchValue>
</platformCore:customField>
<platformCore:customField internalId="1713" scriptId="custitem_apcategoryforsales" xsi:type="platformCore:SearchColumnSelectCustomField">
<platformCore:searchValue internalId="4" typeId="464"/>
</platformCore:customField>
</platformCommon:customFieldList>
</listRel:basic>
<listRel:contactJoin xmlns:platformCommon="urn:common_2012_1.platform.webservices.netsuite.com">
<platformCommon:email>
Expand Down

0 comments on commit 6d0babe

Please sign in to comment.