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

Support non-record field results in saved search responses #426

Open
wants to merge 5 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
28 changes: 25 additions & 3 deletions lib/netsuite/support/search_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,22 @@ def initialize(response, result_class, credentials)
# skip all attributes: look for :basic and all :xxx_join
next if search_group.to_s.start_with?('@')

record[search_group].each_pair do |k, v|
# avoids `RuntimeError: can't add a new key into hash during iteration`
record[search_group][:custom_field_list] ||= {custom_field: []}

record[search_group].each_pair do |attr_name, search_result|
# example pair:
# {
# :department=>{
# :search_value=>{:@internal_id=>"113"},
# :custom_label=>"Business Unit"
# }
# }

# 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 search_result.is_a?(Hash) && search_result.has_key?(:search_value)
# Here's an example of a record ref and string response

# <platformCommon:entity>
Expand All @@ -67,7 +78,18 @@ def initialize(response, result_class, credentials)
# attribute will be transitioned to the parent, and in the case
# of a string response the parent node's value will be to the string

record[search_group][k] = v[:search_value]
if result_class.fields.include?(attr_name) || search_group != :basic
# this is a record field, it will be picked up when we
# intialize the `result_class`
record[search_group][attr_name] = search_result[:search_value]
else
# not a record field -- treat it as if it were a custom field
# otherwise it will be lost when we initialize
custom_fields = record[search_group][:custom_field_list][:custom_field]
custom_fields = [custom_fields] if custom_fields.is_a?(Hash)
custom_fields << search_result.merge(internal_id: attr_name)
record[search_group][:custom_field_list][:custom_field] = custom_fields
end
else
# NOTE need to understand this case more, in testing, only the namespace definition hits this condition
end
Expand Down
22 changes: 22 additions & 0 deletions spec/netsuite/actions/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@
expect(search.results.first.alt_name).to eq('A Awesome Name')
expect(search.results.last.email).to eq('[email protected]')
end

it "should handle an ID search with basic non-field result columns" do
response = File.read('spec/support/fixtures/search/saved_search_item.xml')
savon.expects(:search)
.with(message: {
"searchRecord"=>{
"@xsi:type" =>"listAcct:ItemSearchAdvanced",
"@savedSearchId" =>42,
:content! =>{"listAcct:criteria"=>{}},
}
}).returns(response)
search = NetSuite::Records::InventoryItem.search(saved: 42)
results = search.results
custom_fields = results.map do |record|
record.custom_field_list.custom_fields.map(&:internal_id)
end.flatten.uniq
[
:location_quantity_available,
:location_re_order_point,
:location_quantity_on_order,
].each {|field| expect(custom_fields).to include(field)}
end
end

context "advanced search" do
Expand Down
6 changes: 3 additions & 3 deletions spec/netsuite/records/custom_field_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
context 'writing convience methods' do
it "should create a custom field entry when none exists" do
list.custrecord_somefield = 'a value'
list.custom_fields.size.should == 1
list.custom_fields.first.value.should == 'a value'
list.custom_fields.first.type.should == 'platformCore:StringCustomFieldRef'
expect(list.custom_fields.size).to eq(1)
expect(list.custom_fields.first.value).to eq('a value')
expect(list.custom_fields.first.type).to eq('platformCore:StringCustomFieldRef')
end

# https://github.com/NetSweet/netsuite/issues/325
Expand Down
Loading