3

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.

2
  • The Bulk API Developer's Guide might be helpful. In particular, Chapter 9 on Bulk Query and Chapter 2 Steps 5 (Checking Batch Status) and 6 (Retrieving Batch Result). I'm not familiar with Ruby or the Gem you have linked to understand how the Batch API results should be retrieved. Commented Jul 2, 2013 at 2:50
  • I did the research but I wanted to avoid building a client myself if something already exists. Finally I solved it and added the answer. Thanks!
    – arturodz
    Commented Jul 3, 2013 at 0:13

2 Answers 2

1

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
0

To make things easier I've created salesforce_bulk_query gem: https://github.com/cvengros/salesforce_bulk_query

With this gem you can make a query with a single method call:

result = bulk_api.query("Task", "SELECT Id, Name FROM Task")

All the jobs, batches, etc. are created automatically for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .