Skip to content

Commit

Permalink
Bugfix: Transform NULL values for SF Bulk API, which expects "#N/A" (#74
Browse files Browse the repository at this point in the history
)

* Transform NULL values for SF Bulk API, which expects "#N/A"

* Store NULL_VALUE as a constant.

* Update changelog.
  • Loading branch information
asedge authored Nov 22, 2023
1 parent 8fbd5bd commit 6332168
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 18 additions & 1 deletion lib/active_force/bulk/records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
22 changes: 22 additions & 0 deletions spec/active_force/bulk/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6332168

Please sign in to comment.