From 6332168033882cb247b599fa7487e6b09e723f95 Mon Sep 17 00:00:00 2001 From: Sean Edge Date: Wed, 22 Nov 2023 10:56:35 -0500 Subject: [PATCH] Bugfix: Transform NULL values for SF Bulk API, which expects "#N/A" (#74) * Transform NULL values for SF Bulk API, which expects "#N/A" * Store NULL_VALUE as a constant. * Update changelog. --- CHANGELOG.md | 1 + lib/active_force/bulk/records.rb | 19 ++++++++++++++++++- spec/active_force/bulk/record_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74a75db..6b1c67a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Not released - Change `.first` to not query the API if records have already been retrieved (https://github.com/Beyond-Finance/active_force/pull/73) +- Bugfix: Transform NULL values for SF Bulk API, which expects "#N/A" (https://github.com/Beyond-Finance/active_force/pull/74) ## 0.19.0 diff --git a/lib/active_force/bulk/records.rb b/lib/active_force/bulk/records.rb index c99299c..5a16d97 100644 --- a/lib/active_force/bulk/records.rb +++ b/lib/active_force/bulk/records.rb @@ -3,6 +3,8 @@ module ActiveForce module Bulk class Records + NULL_VALUE = '#N/A'.freeze + attr_reader :headers, :data def initialize(headers:, data:) @headers = headers @@ -16,10 +18,25 @@ def to_csv end def self.parse_from_attributes(records) + # Sorting ensures that the headers line up with the values for the CSV headers = records.first.keys.sort.map(&:to_s) - data = records.map { |r| r.sort.pluck(-1) } + data = records.map do |r| + r.transform_values { |v| transform_value_for_sf(v) }.sort.pluck(-1) + end new(headers: headers, data: data) end + + # SF expects a special value for setting a column to be NULL. + def self.transform_value_for_sf(value) + case value + when NilClass + NULL_VALUE + when Time + value.iso8601 + else + value.to_s + end + end end end end \ No newline at end of file diff --git a/spec/active_force/bulk/record_spec.rb b/spec/active_force/bulk/record_spec.rb index 5ee0467..7cfccbf 100644 --- a/spec/active_force/bulk/record_spec.rb +++ b/spec/active_force/bulk/record_spec.rb @@ -9,11 +9,13 @@ %w[value3 value4], ] end + describe '#to_csv' do it 'returns CSV with headers' do expect(subject.to_csv).to eq "header1,header2\nvalue1,value2\nvalue3,value4\n" end end + describe '::parse_from_attributes' do subject { described_class.parse_from_attributes(attributes) } let(:attributes) do @@ -28,5 +30,25 @@ expect(records.headers).to eq headers expect(records.data).to eq data end + + context 'when there are NULL values' do + let(:attributes) do + [ + { header1: nil, header2: 'value2'}, + { header1: 'value3', header2: nil}, + ] + end + let(:data) do + [ + %w[#N/A value2], + %w[value3 #N/A], + ] + end + + it 'substitutes the expected SF value for NULL' do + records = subject + expect(records.data).to eq data + end + end end end