Update XERO Item after fetching item fails

102 Views Asked by At

I am using the xeroizer gem and facing some issues.

Here is what I have in my controller:

items_found = Item.where(code: codes)
if items_found
      xero.Item.batch_save do
      binding.pry
        items_found.each do |existing_item|
          if existing_item.xero_id
            item = xero.Item.find(existing_item.xero_id)
            item.description     = existing_item.description
            item.unit_price      = existing_item.rate
          else
            item = xero.Item.first(where: { code: existing_item.code })
            item.description     = existing_item.description
            item.unit_price      = existing_item.rate
        end
    end
  end
end

The items_found array contains all items that are already stored and exist in xero, which i get from an array of codes. If the xero_id is stored I try to find the Item but it fails.

However my code fails with this error, after the binding.pry call

Xeroizer::ApiException - PostDataInvalidException: The element 'CogsAccountCode' was not recognised. Ensure the element name has the correct case and that there are no duplicate elements of the same name. 
 Generated by the following XML: 
 <ApiException xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ErrorNumber>14</ErrorNumber>
  <Type>PostDataInvalidException</Type>
  <Message>The element 'CogsAccountCode' was not recognised. Ensure the element name has the correct case and that there are no duplicate elements of the same name.</Message>
</ApiException>:
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:197:in `handle_error!'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:116:in `http_request'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/http.rb:41:in `http_post'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/application_http_proxy.rb:24:in `http_post'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:167:in `block (2 levels) in save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:165:in `block in save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:164:in `save_records'
   () home/alex/.rvm/gems/ruby-2.3.5/bundler/gems/xeroizer-2d305fbdfdb1/lib/xeroizer/record/base_model.rb:191:in `batch_save'
  app/controllers/xero_session_controller.rb:286:in `create_items'

I have found this similar error

There is no mention of CogsAccountCode on the xeroizer gem documentation/code.

Any help would be greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

There was a typo in my codes array which (strangely) was not being caught and was still populating items_found.

As soon as I fixed that and codes where properly inserted, the error went away.