Skip to content

Commit

Permalink
First cut of BulkTK
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Patterson committed Mar 9, 2015
1 parent 09f0f33 commit 5896993
Show file tree
Hide file tree
Showing 11 changed files with 675 additions and 0 deletions.
5 changes: 5 additions & 0 deletions BulkAPISampleFiles/delete.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Id
003E000001DJOfxIAH
003E000001DJOfyIAH
003E000001DJOV4IAP
003E000001DJOV5IAP
16 changes: 16 additions & 0 deletions BulkAPISampleFiles/delete.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Contact records -->
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<sObject>
<Id>003E000001DJOfxIAH</Id>
</sObject>
<sObject>
<Id>003E000001DJOfyIAH</Id>
</sObject>
<sObject>
<Id>003E000001DJOV4IAP</Id>
</sObject>
<sObject>
<Id>003E000001DJOV5IAP</Id>
</sObject>
</sObjects>
3 changes: 3 additions & 0 deletions BulkAPISampleFiles/insert.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FirstName,LastName,Department,Birthdate,Description
Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast"
Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."
17 changes: 17 additions & 0 deletions BulkAPISampleFiles/insert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Contact records -->
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<sObject>
<FirstName>Tom</FirstName>
<LastName>Jones</LastName>
<Department>Marketing</Department>
<Birthdate>1940-06-07Z</Birthdate>
<Description>Self-described as "the top" branding guru on the West Coast</Description>
</sObject>
<sObject>
<FirstName>Ian</FirstName>
<LastName>Dury</LastName>
<Department>R&amp;D</Department>
<Description>World-renowned expert in fuzzy logic design. Influential in technology purchases.</Description>
</sObject>
</sObjects>
2 changes: 2 additions & 0 deletions BulkAPISampleFiles/update.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Id,Title
003E000001CbzBi,Ninja
8 changes: 8 additions & 0 deletions BulkAPISampleFiles/update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Contact records -->
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<sObject>
<Id>003E000001CbzBi</Id>
<Title>Pirate</Title>
</sObject>
</sObjects>
2 changes: 2 additions & 0 deletions BulkAPISampleFiles/upsert.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Twitter_Handle__c,Title
pattest7,Guru
8 changes: 8 additions & 0 deletions BulkAPISampleFiles/upsert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Contact records -->
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<sObject>
<Twitter_Handle__c>pattest7</Twitter_Handle__c>
<Title>Pirate</Title>
</sObject>
</sObjects>
121 changes: 121 additions & 0 deletions BulkTK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
BulkTK: Force.com Bulk API JavaScript Toolkit
=============================================

This minimal toolkit extends ForceTK to allow JavaScript in web pages to call the [Force.com Bulk API](https://www.salesforce.com/us/developer/docs/api_asynch/).

Background
==========

The Force.com Bulk API allows asynchronous data access. BulkTK extends ForceTK with methods to create Bulk API jobs, add batches to them, monitor job status and retrieve job results. Control plane XML is parsed to JavaScript objects for ease of use, while data is returned verbatim.

You should familiarize yourself with the [Force.com Bulk API documentation](https://www.salesforce.com/us/developer/docs/api_asynch/), since BulkTK is a relatively thin layer on the raw XML Bulk API.

bulk.page is a simple Visualforce single page application to demonstrate BulkTK. Try it out in a sandbox or developer edition.

Note that, just like ForceTK, BulkTK is unsupported and supplied as is. It is also currently in a very early stage of development. It appears to work well, but bugs cannot be ruled out, and the interface should not be considered stable.

Example Usage
=============

You must create a ForceTK client first. On a Visualforce page, you would do:

var client = new forcetk.Client();
client.setSessionToken('{!$Api.Session_ID}');

See the ForceTK documentation for authenticating from an external website, such as a Heroku app, or PhoneGap/Cordova.

Create a job
------------

// See https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_reference_jobinfo.htm
// for details of the JobInfo structure

// Insert Contact records in CSV format
var job = {
operation : 'insert',
object : 'Contact',
contentType : 'CSV'
};

client.createJob(job, function(response) {
jobId = response.jobInfo.id;
console.log('Job created with id '+jobId+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error creating job', jqXHR.responseText);
});

Add a batch of records to the job
---------------------------------

You can add multiple batches to the job; each batch can contain up to 10,000 records. See [batch size and limits](https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_concepts_limits.htm#batch_size_title) for more details.

var csvData = "FirstName,LastName,Department,Birthdate,Description\n"+
"Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast\n"+
"Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."\n";

client.addBatch(jobId, "text/csv; charset=UTF-8", csvData,
function(response){
console.log('Added batch '+response.batchInfo.id+'. State: '+response.batchInfo.state+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error adding batch', jqXHR.responseText);
});

See BulkAPISampleFiles for sample CSV and XML data for different operations.

Close the job
-------------

You must close the job to inform Salesforce that no more batches will be submitted for the job.

client.closeJob(jobId, function(response){
console.log('Job closed. State: '+response.jobInfo.state+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error closing job', jqXHR.responseText);
});

Check batch status
------------------

client.getBatchDetails(jobId, batchId, function(response){
console.log('Batch state: '+response.batchInfo.state+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting batch details', jqXHR.responseText);
});

Get batch results
-----------------

Pass `true` as the `parseXML` parameter to get batch results for a query, false otherwise.

client.getBatchResult(jobId, batchId, false, function(response){
console.log('Batch result: '+response);
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting batch result', jqXHR.responseText);
});

Bulk query
----------

When adding a batch to a bulk query job, the `contentType` for the request must be either `text/csv` or `application/xml`, depending on the content type specified when the job was created. The actual SOQL statement supplied for the batch will be in plain text format.

var soql = 'SELECT Id, FirstName, LastName, Email FROM Contact';

client.addBatch(jobId, 'text/csv', soql, function(response){
console.log('Batch state: '+response.batchInfo.state+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting batch result', jqXHR.responseText);
});

Getting bulk query results is a two step process. Call `getBatchResult()` with `parseXML` set to `true` to get a set of result IDs, then call `getBulkQueryResult()` to get the actual records for each result

client.getBatchResult(jobId, batchId, true, function(response){
response['result-list'].result.forEach(function(resultId){
client.getBulkQueryResult(jobId, batchId, resultId, function(response){
console.log('Batch result: '+response);
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting bulk query results', jqXHR.responseText);
});
});
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting batch result', jqXHR.responseText);
});
Loading

0 comments on commit 5896993

Please sign in to comment.