|
| 1 | +# https://system.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteCloudCustomizationScriptingWebServices/SuiteTalkWebServices/AsynchronousRequestProcessing.html |
| 2 | +module NetSuite |
| 3 | + module Actions |
| 4 | + class AsyncAddList |
| 5 | + include Support::Requests |
| 6 | + |
| 7 | + def initialize(objects) |
| 8 | + @objects = objects |
| 9 | + end |
| 10 | + |
| 11 | + private |
| 12 | + |
| 13 | + def request(credentials={}) |
| 14 | + NetSuite::Configuration.connection({element_form_default: :unqualified}, credentials).call(:async_add_list, message: request_body) |
| 15 | + end |
| 16 | + |
| 17 | + # <soap:Body> |
| 18 | + # <asyncAddList> |
| 19 | + # <record xsi:type="listRel:Customer" externalId="ext1"> |
| 20 | + # <listRel:entityId>Shutter Fly</listRel:entityId> |
| 21 | + # <listRel:companyName>Shutter Fly, Inc</listRel:companyName> |
| 22 | + # </record> |
| 23 | + # <record xsi:type="listRel:Customer" externalId="ext2"> |
| 24 | + # <listRel:entityId>Target</listRel:entityId> |
| 25 | + # <listRel:companyName>Target</listRel:companyName> |
| 26 | + # </record> |
| 27 | + # </asyncAddList> |
| 28 | + # </soap:Body> |
| 29 | + def request_body |
| 30 | + attrs = @objects.map do |o| |
| 31 | + hash = o.to_record.merge({ |
| 32 | + '@xsi:type' => o.record_type |
| 33 | + }) |
| 34 | + if o.respond_to?(:external_id) && o.external_id |
| 35 | + hash['@externalId'] = o.external_id |
| 36 | + end |
| 37 | + hash |
| 38 | + end |
| 39 | + { 'record' => attrs } |
| 40 | + end |
| 41 | + |
| 42 | + #<soapenv:Body> |
| 43 | + # <asyncAddListResponse xmlns="urn:messages_2_5.platform.webservices.netsuite.com"> |
| 44 | + # <asyncStatusResult xmlns="urn:core_2_5.platform.webservices.netsuite.com"> |
| 45 | + # <jobId>ASYNCWEBSERVICES_563214_053120061943428686160042948_4bee0685</jobId> |
| 46 | + # <status>pending</status> |
| 47 | + # <percentCompleted>0.0</percentCompleted> |
| 48 | + # <estRemainingDuration>0.0</estRemainingDuration> |
| 49 | + # </asyncStatusResult> |
| 50 | + # </asyncAddListResponse> |
| 51 | + #</soapenv:Body> |
| 52 | + def response_body |
| 53 | + @response_body ||= begin |
| 54 | + response_hash = @response.to_hash |
| 55 | + response_hash[:async_add_list_response] ? response_hash[:async_add_list_response][:async_status_result] : nil |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def success? |
| 60 | + !response_body.nil? |
| 61 | + end |
| 62 | + |
| 63 | + module Support |
| 64 | + |
| 65 | + def self.included(base) |
| 66 | + base.extend(ClassMethods) |
| 67 | + end |
| 68 | + |
| 69 | + module ClassMethods |
| 70 | + def async_add_list(objects = [], credentials = {}) |
| 71 | + objects_list = objects.map do |object| |
| 72 | + object.kind_of?(self) ? object : self.new(object) |
| 73 | + end |
| 74 | + response = NetSuite::Actions::AsyncAddList.call([objects_list], credentials) |
| 75 | + response.success? ? NetSuite::Async::Status.new(response.body) : false |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments