Module: SfCli::Sf::Data::UpsertBulk

Defined in:
lib/sf_cli/sf/data/upsert_bulk.rb

Instance Method Summary collapse

Instance Method Details

#upsert_bulk(file:, sobject:, external_id:, wait: nil, target_org: nil, api_version: nil) ⇒ JobInfo, BulkResultV2

Update records using Bulk API 2.0

Examples:

# start a upsert job
jobinfo = sf.data.upsert_bulk sobject: :TestCustomObject__c, file: 'upsert.csv' # this returns immediately
jobinfo.id  # => "750J4000003g1OaIAI" it's job ID

# you can check if the upsert job completed
sf.data.upsert_resume job_id: jobinfo.id

# Or, you can wait for the job completion with one try.
result = sf.data.upsert_bulk sobject: :TestCustomObject__c, file: 'upsert.csv', wait: 5  # wait within 5 minutes

# you can use IO-like object, which has #read, to `file` keyword:
require 'stringio'
csv = StringIO.new <<CSV
  Id,Name
  001J400000Ki61uIAB,John Smith
  001J400000Ki3WRIAZ,Foo Baz Bar
CSV
jobinfo = sf.data.upsert_bulk sobject: :TestCustomObject__c, file: csv

Parameters:

  • file (String, #read)

    (1)path of a CSV file, which is written record IDs to delete. (2) IO-like object, which has #read method

  • sobject (Symbol, String)

    object type(ex. Account)

  • external_id (String)

    name of the external ID field.Otherwise it must be Id

  • wait (Integer) (defaults to: nil)

    max minutes to wait for the job complete the task

  • target_org (Symbol, String) (defaults to: nil)

    an alias of paticular org, or username can be used

  • api_version (Numeric) (defaults to: nil)

    override the api version used for api requests made by this command

Returns:

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sf_cli/sf/data/upsert_bulk.rb', line 39

def upsert_bulk(file:, sobject:, external_id:, wait: nil, target_org: nil, api_version: nil)
  _file = create_tmpfile_by_io(file)
  path  = _file&.path || file
  flags = {
    :"file"        => path,
    :"sobject"     => sobject,
    :"external-id" => external_id,
    :"wait"        => wait,
    :"target-org"  => target_org,
    :"api-version"  => api_version,
  }
  action = __method__.to_s.tr('_', ' ')
  json = exec(action, flags: flags, redirection: :null_stderr)

  job_info =  ::SfCli::Sf::Data::JobInfo.new(**json['result']['jobInfo'])
  return job_info unless json['result']['records']

  ::SfCli::Sf::Data::BulkResultV2.new(
    job_info: job_info,
    records:  ::SfCli::Sf::Data::BulkRecordsV2.new(**json['result']['records'])
  )
ensure
  _file&.close!
end