This is a repo forked from a very old public repo. github.com/jorgevaldivia/salesforce_bulk
This code is currently used in Clockwork, alongside other Salesforce dependencies.
Our main responsibility is to maintain this code and to ensure safe upgrades.
-
Unit tests were added by us (Justworks) to help capture that there is a contract and to test that the code matches the Salesforce API version’s request / response contract.
-
Each function test is preceded by a link to the Salesforce documentation for the relevant version. This, plus manual testing, is where the request / response payloads come from.
If you have to do an upgrade:
-
Replace the dummy requests / responses with the new version’s requests / responses. Run the unit tests.
bundle exec rspec
-
Manually test. For those of you who generally work in Go, this will entail:
Build your local version of the code:
gem build salesforce_bulk.gemspec
Install that local version:
gem install salesforce_bulk-{version}.gem
e.g.
gem install salesforce_bulk-1.0.2.gem
Start the Ruby interactive session and import the local gem
irb -rsalesforce_bulk
Make an example query (note the version), similar to those in Clockwork:
SalesforceBulk::Api.new("USERNAME_YOU_CAN_TAKE_FROM_NEON" "PASSWORD_YOU_CAN_TAKE_FROM_NEON", api_version="34.0", in_sandbox=true).query("Lead", "select Id, Company_ID__c, OwnerId, IsConverted from Lead where Company_ID__c != null limit 5")
Salesforce bulk is a simple ruby gem for connecting to and using the Salesforce Bulk API (www.salesforce.com/us/developer/docs/api_asynch/index.htm). There are already some gems out there that provide connectivity to the Salesforce SOAP and Rest APIs, if your needs are simple, I recommend using those, specifically raygao’s asf-rest-adapter (github.com/raygao/asf-rest-adapter) or databasedotcom (rubygems.org/gems/databasedotcom).
sudo gem install salesforce_bulk
Using this gem is simple and straight forward.
To initialize:
require 'salesforce_bulk' salesforce = SalesforceBulk::Api.new("YOUR_SALESFORCE_USERNAME", "YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_TOKEN")
To use sandbox:
salesforce = SalesforceBulk::Api.new("YOUR_SALESFORCE_SANDBOX_USERNAME", "YOUR_SALESFORCE_PASSWORD+YOUR_SALESFORCE_SANDBOX_TOKEN", true)
Note: the second parameter is a combination of your Salesforce token and password. So if your password is xxxx and your token is yyyy, the second parameter will be xxxxyyyy
Sample operations:
# Insert/Create new_account = Hash["name" => "Test Account", "type" => "Other"] # Add as many fields per record as needed. records_to_insert = Array.new records_to_insert.push(new_account) # You can add as many records as you want here, just keep in mind that Salesforce has governor limits. result = salesforce.create("Account", records_to_insert) puts "result is: #{result.inspect}" # Update updated_account = Hash["name" => "Test Account -- Updated", "id" => "a00A0001009zA2m"] # Nearly identical to an insert, but we need to pass the salesforce id. records_to_update = Array.new records_to_update.push(updated_account) salesforce.update("Account", records_to_update) # Upsert upserted_account = Hash["name" => "Test Account -- Upserted", "External_Field_Name" => "123456"] # Fields to be updated. External field must be included records_to_upsert = Array.new records_to_upsert.push(upserted_account) salesforce.upsert("Account", records_to_upsert, "External_Field_Name") # Note that upsert accepts an extra parameter for the external field name OR salesforce.upsert("Account", records_to_upsert, "External_Field_Name", true) # last parameter indicates whether to wait until the batch finishes # Delete deleted_account = Hash["id" => "a00A0001009zA2m"] # We only specify the id of the records to delete records_to_delete = Array.new records_to_delete.push(deleted_account) salesforce.delete("Account", records_to_delete) # Query res = salesforce.query("Account", "select id, name, createddate from Account limit 3") # We just need to pass the sobject name and the query string puts res.result.records.inspect
Result reporting:
new_account = Hash["type" => "Other"] # Missing required field "name." records_to_insert = Array.new records_to_insert.push(new_account) # You can add as many records as you want here, just keep in mind that Salesforce has governor limits. result = salesforce.create("Account", records_to_insert, true) # Result reporting can only be used if wait is set to true puts result.success? # false puts result.has_errors? # true puts result.result.errors # An indexed hash detailing the errors
Copyright © 2012 Jorge Valdivia.
