function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
micahmicah 

Bulk API queries aren't returning any records

I'm trying to use the Bulk API to execute a query and get ~230,000 records back. But when I execute the query, instead I get a cryptic id number and no records at all. Can anyone help?

I'm using both the Force.com Toolkit for PHP and the PHP Bulk API found here: http://wiki.developerforce.com/index.php/Web_Services_API#PHP

In short, here's what I'm doing:

// connect to Salesforce using Force.com Toolkit for PHP
$sf = new SforceEnterpriseClient();
$sf->createConnection("enterprise.wsdl.xml");
$login = $sf->login($username, $password.$security_token);

// initialize the PHP Bulk API
$sfbulk = new BulkApiClient($login->serverUrl, $login->sessionId);
$sfbulk->setCompressionEnabled(true);

// create a Bulk API job
$job = new JobInfo();
$job->setObject('Contact');
$job->setOpertion('query');
$job->setContentType('CSV');
$job->setConcurrencyMode('Parallel');
$job = $sfbulk->createJob($job);

// add the batch and close the job
$batch = $sfbulk->createBatch($job, "SELECT Id, FirstName FROM Contact LIMIT 10");
$sfbulk->updateJobState($job->getId(), "Closed");

// monitor the batch status until it's done
while($batch->getState() == "Queued" || $batch->getState() == "InProgress") {
    sleep(5);
    $batch = $sfbulk->getBatchInfo($job->getId(), $batch->getId());
}
if($batch->getState() == "Failed") {
    echo("Batch failed!");
    exit();
}

// get the results
$results = $sfbulk->getBatchResults($job->getId(), $batch->getId());
echo("Batch complete! Results: ".json_encode($results));

And the results I'm getting from running this script are:

Batch complete! Results: <result-list xmlns="http://www.force.com/2009/06/asyncapi/dataload"><result>752A0000000GnBh</result></result-list>

I have tried several different queries. Some of the batches that have more complicated joins had a "Failed" status, but this is a really simple query, and I should be getting 10 contacts back. Instead I'm getting "752A0000000GnBh" with no indication of what this is an id for.

When I do the same thing except all with the Force.com Toolkit for PHP it works fine and I get the 10 results I'm looking for. The problem is when I want to select all of the records in the Contact table I run into the API request limit very quickly and can only process about 20,000 records in a single day. It seems like using the Bulk API would fix this (I don't have to constantly make queryMore() requests), but it doesn't appear to be working.

I googled around for a solution and found this blog post: http://blogs.developerforce.com/engineering/2011/03/bulk-api-queries.html

It says the query command in the Bulk API is a pilot feature and "Since this feature is still in pilot, first you will need to contact your Salesforce representative to have it enabled for your organization." I have called custoimer service and they assured me that my Salesforce account does have this enabled. Which would make sense, because it never gave me an error message.

Am I just using the Bulk API wrong? If so, how can I use it to get records from a query? If not, is the Bulk API broken?

Best Answer chosen by Admin (Salesforce Developers) 
dkadordkador

In query mode, one batch may have multiple results.  So your request to /services/async/<version>/job/<id>/batch/<id>/result is returning a list of result IDs (well, in your case the list has only one element).  Then you need to send a request to /services/async/<version>/job/<id>/batch/<id>/result/<result_id> to get your actual queried records.

 

I'm not sure if the PHP toolkit supports this call yet.  Take a look.  It should be pretty easy for you to modify it if it does not.

 

All Answers

dkadordkador

In query mode, one batch may have multiple results.  So your request to /services/async/<version>/job/<id>/batch/<id>/result is returning a list of result IDs (well, in your case the list has only one element).  Then you need to send a request to /services/async/<version>/job/<id>/batch/<id>/result/<result_id> to get your actual queried records.

 

I'm not sure if the PHP toolkit supports this call yet.  Take a look.  It should be pretty easy for you to modify it if it does not.

 

This was selected as the best answer
micahmicah

Thank you! The PHP library for the Bulk API does indeed support this, it just wasn't shown in the sample code. After the batch finishes processing, this works to get the actual csv full of records:

 

$results = simplexml_load_string($sfbulk->getBatchResults($job->getId(), $batch->getId()));
$resultId = $results->result;
$csv = $sfbulk->getBatchResult($job->getId(), $batch->getId(), $resultId);