[SalesForce] How to perform a query to the salesforce bulk API with the salesforce_bulk_api gem

I'm trying to perform a bulk query to salesforce with the salesforce_bulk_api gem. I am connecting successfully and doing the query with no problems but I can't get the actual results.

This is what I do:

$- res = salesforce.query("Account", "select id, name from Account limit 3")
$- puts res

It prints:

<?xml version="1.0" encoding="UTF-8"?><jobInfo
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <id>XXXXXXXXXXXXX</id>
 <operation>query</operation>
 <object>Account</object>
 <createdById>XXXXXXXXXXXXXXX</createdById>
 <createdDate>2013-07-01T16:30:36.000Z</createdDate>
 <systemModstamp>2013-07-01T16:30:36.000Z</systemModstamp>
 <state>Open</state>
 <concurrencyMode>Parallel</concurrencyMode>
 <contentType>XML</contentType>
 <numberBatchesQueued>0</numberBatchesQueued>
 <numberBatchesInProgress>0</numberBatchesInProgress>
 <numberBatchesCompleted>0</numberBatchesCompleted>
 <numberBatchesFailed>0</numberBatchesFailed>
 <numberBatchesTotal>0</numberBatchesTotal>
 <numberRecordsProcessed>0</numberRecordsProcessed>
 <numberRetries>0</numberRetries>
 <apiVersion>23.0</apiVersion>
 <numberRecordsFailed>0</numberRecordsFailed>
 <totalProcessingTime>0</totalProcessingTime>
 <apiActiveProcessingTime>0</apiActiveProcessingTime>
 <apexProcessingTime>0</apexProcessingTime>
</jobInfo>
 => {"xmlns"=>"http://www.force.com/2009/06/asyncapi/dataload", "id"=>["XXXXXXXXXXXX"], "jobId"=>["XXXXXXXXXXXXXXX"], "state"=>["Completed"], "createdDate"=>["2013-07-01T16:30:37.000Z"], "systemModstamp"=>["2013-07-01T16:30:37.000Z"], "numberRecordsProcessed"=>["3"], "numberRecordsFailed"=>["0"], "totalProcessingTime"=>["0"], "apiActiveProcessingTime"=>["0"], "apexProcessingTime"=>["0"]} 

It says the job is completed but I can't figure out how to extract the results.

May I add that I already followed the procedure explained in the documentation of the gem.

Thanks.

Best Answer

I finally found the answer, it looks like the salesforce_bulk_api gem is not very well documented, fortunately the salesforcebulk gem is. The way to solve this is to:

  1. Authenticate
    client = SalesforceBulk::Client.new(username: 'MyUsername', password: 'MyPasswordWithSecurtyToken')

    client.authenticate
  1. Add the job
    job = client.add_job(:query, :Account)
  1. Add the batch to the job
    batch = client.add_batch(job.id, "SELECT Id, Name FROM Account")
  1. Close the job
    job = client.close_job(job.id)
  1. Check the job status
    batch = client.batch_info(job.id, batch.id)
  1. Get results from the batch
    results = client.batch_result(jobId, batchId)
  1. Query over the results
    results.each do |result|
      puts result[:Name]
    end