Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multi-tenancy #3

Open
corytheboyd opened this issue Jan 3, 2017 · 1 comment
Open

Support for multi-tenancy #3

corytheboyd opened this issue Jan 3, 2017 · 1 comment

Comments

@corytheboyd
Copy link

corytheboyd commented Jan 3, 2017

I have a multi-tenant application, and need to create Batch Jobs for many different Salesforce organizations, which means many different Restforce clients. It would be great if the Job creation API supported that, as it's effectively hard-coded to Restforce::Bulk.client as far as I can tell.

From what I can tell, I can do it by basically re-implementing Restforce::Bulk::Job::create, but that isn't very clean:

# My own application code, used to get Restforce::Data::Client instance for a User.
restforce_client = User.first.get_restforce_client

bulk_client = Restforce::Bulk::Client.new(restforce_client)
job_builder = Restforce::Bulk::Builder::Xml.new(:query)
job_data = job_builder.job('Contact', 'CSV')
job_create_response = bulk_client.perform_request(:post, 'job', job_data)
job = Restforce::Bulk::Job.new(job_create_response.body.jobInfo)

Almost seems like that should be:

bulk_client = Restforce::Bulk::Client.new(restforce_client)
job = bulk_client.create_job(:query, 'Account', :csv)
@corytheboyd
Copy link
Author

corytheboyd commented Jan 4, 2017

Here is the working code I came up with, basically avoiding the ::create methods exposed by Restforce::Bulk Job and Batch classes

# Collect Salesforce Contact IDs from our database
contact_ids = []
Person.find_in_batches do |people|
  contact_ids += people.map(&:sfdc_id).select { |id| id.starts_with?('003') }
end

# Create Bulk Job. Very long winded
restforce_client = User.first.get_restforce_client
bulk_client = Restforce::Bulk::Client.new(restforce_client)

job_builder = Restforce::Bulk::Builder::Xml.new(:query)
job_data = job_builder.job('Contact', 'CSV')
job_create_response = bulk_client.perform_request(:post, 'job', job_data)
job = Restforce::Bulk::Job.new(job_create_response.body.jobInfo)

batch_builder = Restforce::Bulk::Builder::Csv.new(:query)
batches = []

contact_ids.in_groups_of(200, false) do |contact_ids_group|
  contact_ids_range_string =
    contact_ids_group.map { |id| "'#{id}'" }.join(',')

  soql = <<-SOQL
  Select Id, AccountId FROM Contact
  SOQL

  add_batch_response = bulk_client.perform_request(
    :post,
    "job/#{job.id}/batch",
    soql,
    :csv
  )
  batch = Restforce::Bulk::Batch.new(add_batch_response.body.batchInfo)

  batches << batch
end

It's still pretty clean, to be honest. I still think the need stands to officially support this. I wouldn't mind taking a crack at it when I have some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant