-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
aom
wants to merge
8
commits into
NetSweet:master
Choose a base branch
from
smartlyio:actions-add-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2c973a7
Merge pull request #1 from NetSweet/master
aom 18bb31a
Merge remote-tracking branch 'upstream/master'
aom 979f6d1
Merge remote-tracking branch 'upstream/master'
aom b552cea
Adds AddList to Actions
aom aa17e2d
Test for duplicate entity
aom 0382c36
Fetch external id from @objects if it doesn't exist in status array
aom a03b3ea
Change AddList action to be included when :add_list keyword is present
aom d532112
Add :add_list action to all records with :add action
aom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
end | ||
|
||
netsuite_records | ||
else | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 benil
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 fromaddList
to generate a new list of records to return. This would eliminate thefind
usage andinstance_variable_set
.What do you think? I haven't worked with
addList
so I could be wrong here.There was a problem hiding this comment.
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 fornil
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).There was a problem hiding this comment.
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.