Skip to content

Bulk CSV generation drops fields that first appear after the first record #1811

Open

Description

@Smoke221

Bulk CSV generation drops fields that first appear after the first record

Summary

When JSforce generates CSV from an array of records, fields that are absent from the first record but present in later records are omitted from the generated CSV.

This happens because csv-stringify infers the CSV columns from the first record when no explicit columns option is provided. Any fields introduced by later records are silently dropped from the header and output rows.

Reproduction

toCSV([
  { Name: 'First' },
  { Name: 'Second', Extra__c: 'only later' },
  { Name: 'Third', Extra__c: 'another value' },
  { Name: 'Fourth' },
  { Name: 'Fifth', Extra__c: 'last value', Extra2__c: 'never used' },
]);

Current Behavior

The generated CSV only includes the columns inferred from the first record:

Name
First
Second
Third
Fourth
Fifth

Fields such as Extra__c and Extra2__c are dropped.

Expected Behavior

The generated CSV should include the union of columns across all records:

Name,Extra__c,Extra2__c
First,,
Second,only later,
Third,another value,
Fourth,,
Fifth,last value,never used

Impact

This can silently omit data during CSV generation. It is especially risky for Bulk and Bulk2 array uploads, where records may not all have the same shape.

For example, a later record may contain an optional Salesforce field that is missing from the first record. In that case, the field value is not included in the uploaded CSV, which can lead to missing data, validation failures, or incorrect records.

Suggested Fix

Compute the union of CSV columns across all prepared records and pass those columns explicitly to csv-stringify.

This avoids relying on first-record column inference and preserves fields that appear only in later records.

Regression Test

A regression test should verify that fields first introduced by later records are included in both the generated header and the generated rows:

import assert from 'assert';
import { getCSVColumns, toCSV } from '../src/csv';

it('should include columns that first appear in later records', () => {
  const records = [
    { Name: 'First' },
    { Name: 'Second', Extra__c: 'only later' },
    { Name: 'Third', Extra__c: 'another value' },
    { Name: 'Fourth' },
    { Name: 'Fifth', Extra__c: 'last value', Extra2__c: 'never used' },
  ];

  assert.deepStrictEqual(getCSVColumns(records), [
    'Name',
    'Extra__c',
    'Extra2__c',
  ]);
  assert.strictEqual(
    toCSV(records),
    [
      'Name,Extra__c,Extra2__c',
      'First,,',
      'Second,only later,',
      'Third,another value,',
      'Fourth,,',
      'Fifth,last value,never used',
      '',
    ].join('\n'),
  );
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions