Skip to content

Code Snippets Examples

Diego Polido Santana edited this page Oct 22, 2021 · 2 revisions

I'm creating this page to share some code snippets that took longer to build due to lack of documentation and may be useful for everyone who will use this gem

Building Journal Entry Record with custom_field to add/upsert/update.

        lines = @results.map do |result_row|
          line = {
            account: { internal_id: internal_id('adjustment_account', result_row['account']) },
            memo: result_row['note'],
            department: {
              internal_id: internal_id('department', 'Finance')
            },
            klass: {
              internal_id: internal_id('order_type', result_row['order_type'])
            },
            custom_field_list: {
              custom_field: {
                script_id: 'custcol_fleaf_item',
                type: 'platformCore:SelectCustomFieldRef',
                value: {
                  internal_id: internal_id('item', result_row['item']),
                  type: 'platformCore:ListOrRecordRef'
                }
              }
            }
          }

          line[:debit] = result_row['debit_amount'] if result_row['debit_amount'].present?
          line[:credit] = result_row['credit_amount'] if result_row['credit_amount'].present?

          line
        end


        record_object = ::NetSuite::Records::JournalEntry.new(
          external_id: first_row['external_id'],
          memo: self.class.name.demodulize,
          currency: { internal_id: internal_id('currency', first_row['currency']) },
          tran_date: ::NetSuite::Utilities.normalize_time_to_netsuite_date(first_row['date'].to_time.utc.to_i),
          subsidiary: {
            internal_id: internal_id('subsidiary', first_row['subsidiary'])
          },
          custom_field_list: {
            custom_field: {
              script_id: 'custbody_wipfli_je_type',
              type: 'platformCore:SelectCustomFieldRef',
              value: {
                internal_id: internal_id('journal_entry_type', first_row['journal_entry_type']),
                type: 'platformCore:ListOrRecordRef'
              }
            }
          },
          line_list: {
            line: lines
          }
        )

Building Inventory Adjustment Record with custom_field to add/upsert/update.

        inventories = results.map do |result_row|
          internal_location_id = internal_id('location', result_row['location'])
          {
            item: {
              internal_id: internal_id('item', result_row['item'])
            },
            location: {
              internal_id: internal_location_id
            },
            adjust_qty_by: result_row['adjust_qty_by'],
            inventory_detail: {
              inventory_assignment_list: {
                inventory_assignment: [
                  {
                    issue_inventory_number: {
                      internal_id: internal_id('inventory_number', result_row['item'], internal_location_id: internal_location_id)
                    },
                    quantity: result_row['adjust_qty_by']
                  },
                ]
              }
            }
          }
        end

        record_object = ::NetSuite::Records::InventoryAdjustment.new(
          external_id: first_row['external_id'],
          memo: self.class.name.demodulize,
          tran_date: ::NetSuite::Utilities.normalize_time_to_netsuite_date(first_row['date'].to_time.utc.to_i),
          account: {
            internal_id: internal_id('adjustment_account', first_row['adjustment_account'])
          },
          subsidiary: {
            internal_id: internal_id('subsidiary', first_row['subsidiary'])
          },
          custom_field_list: {
            custom_field: {
              script_id: 'custbody_fleaf_reason_code',
              type: 'platformCore:SelectCustomFieldRef',
              value: {
                internal_id: internal_id('reason_code', first_row['reason_code']),
                type: 'platformCore:ListOrRecordRef'
              }
            }
          },
          inventory_list: {
            inventory: inventories
          },
        )