Skip to content

Commit

Permalink
Merge pull request jorgevaldivia#8 from tonyjiang/master
Browse files Browse the repository at this point in the history
Two improvements
  • Loading branch information
jorgevaldivia committed Jun 24, 2012
2 parents cc0887c + cfc6966 commit 2cf79fd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ 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:
Expand All @@ -35,6 +38,10 @@ Sample operations:
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
Expand Down
44 changes: 22 additions & 22 deletions lib/salesforce_bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ module SalesforceBulk
# Your code goes here...
class Api

@@SALESFORCE_API_VERSION = '23.0'
@@SALESFORCE_API_VERSION = '24.0'

def initialize(username, password)
@connection = SalesforceBulk::Connection.new(username, password, @@SALESFORCE_API_VERSION)
def initialize(username, password, in_sandbox=false)
@connection = SalesforceBulk::Connection.new(username, password, @@SALESFORCE_API_VERSION, in_sandbox)
end

def upsert(sobject, records, external_field)
self.do_operation('upsert', sobject, records, external_field)
def upsert(sobject, records, external_field, wait=false)
self.do_operation('upsert', sobject, records, external_field, wait)
end

def update(sobject, records)
Expand All @@ -35,9 +35,7 @@ def query(sobject, query)
self.do_operation('query', sobject, query, nil)
end

#private

def do_operation(operation, sobject, records, external_field)
def do_operation(operation, sobject, records, external_field, wait=false)
job = SalesforceBulk::Job.new(operation, sobject, records, external_field, @connection)

# TODO: put this in one function
Expand All @@ -49,22 +47,24 @@ def do_operation(operation, sobject, records, external_field)
end
job.close_job()

while true
state = job.check_batch_status()
#puts "\nstate is #{state}\n"
if state != "Queued" && state != "InProgress"
break
if wait or operation == 'query'
while true
state = job.check_batch_status()
#puts "\nstate is #{state}\n"
if state != "Queued" && state != "InProgress"
break
end
sleep(2) # wait x seconds and check again
end

if state == 'Completed'
job.get_batch_result()
else
return "There is an error in your job."
end
sleep(2) # wait x seconds and check again
end

if state == 'Completed'
job.get_batch_result()
else
return "error"
return "The job has been closed."
end

end

end # End class
end
end # End module
3 changes: 2 additions & 1 deletion lib/salesforce_bulk/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Connection
@@LOGIN_HOST = 'login.salesforce.com'
@@INSTANCE_HOST = nil # Gets set in login()

def initialize(username, password, api_version)
def initialize(username, password, api_version, in_sandbox)
@username = username
@password = password
@session_id = nil
Expand All @@ -16,6 +16,7 @@ def initialize(username, password, api_version)
@@API_VERSION = api_version
@@LOGIN_PATH = "/services/Soap/u/#{@@API_VERSION}"
@@PATH_PREFIX = "/services/async/#{@@API_VERSION}/"
@@LOGIN_HOST = 'test.salesforce.com' if in_sandbox

login()
end
Expand Down
5 changes: 2 additions & 3 deletions lib/salesforce_bulk/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ def add_query
end

def add_batch()
keys = @@records.reduce({}) {|h,pairs| pairs.each {|k,v| (h[k] ||= []) << v}; h}.keys
headers = keys.to_csv
keys = @@records.first.keys

output_csv = headers
output_csv = keys.to_csv

@@records.each do |r|
fields = Array.new
Expand Down

0 comments on commit 2cf79fd

Please sign in to comment.