Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit e5428de

Browse files
authored
Merge pull request #1 from developerforce/master
Pending merge
2 parents 8b18f9b + 01974a1 commit e5428de

31 files changed

+1768
-1176
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

BulkAPISampleFiles/delete.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Id
2+
003E000001DJOfxIAH
3+
003E000001DJOfyIAH
4+
003E000001DJOV4IAP
5+
003E000001DJOV5IAP

BulkAPISampleFiles/delete.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Contact records -->
3+
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
4+
<sObject>
5+
<Id>003E000001DJOfxIAH</Id>
6+
</sObject>
7+
<sObject>
8+
<Id>003E000001DJOfyIAH</Id>
9+
</sObject>
10+
<sObject>
11+
<Id>003E000001DJOV4IAP</Id>
12+
</sObject>
13+
<sObject>
14+
<Id>003E000001DJOV5IAP</Id>
15+
</sObject>
16+
</sObjects>

BulkAPISampleFiles/insert.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FirstName,LastName,Department,Birthdate,Description
2+
Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast"
3+
Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."

BulkAPISampleFiles/insert.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Contact records -->
3+
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
4+
<sObject>
5+
<FirstName>Tom</FirstName>
6+
<LastName>Jones</LastName>
7+
<Department>Marketing</Department>
8+
<Birthdate>1940-06-07Z</Birthdate>
9+
<Description>Self-described as "the top" branding guru on the West Coast</Description>
10+
</sObject>
11+
<sObject>
12+
<FirstName>Ian</FirstName>
13+
<LastName>Dury</LastName>
14+
<Department>R&amp;D</Department>
15+
<Description>World-renowned expert in fuzzy logic design. Influential in technology purchases.</Description>
16+
</sObject>
17+
</sObjects>

BulkAPISampleFiles/update.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Id,Title
2+
003E000001CbzBi,Ninja

BulkAPISampleFiles/update.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Contact records -->
3+
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
4+
<sObject>
5+
<Id>003E000001CbzBi</Id>
6+
<Title>Pirate</Title>
7+
</sObject>
8+
</sObjects>

BulkAPISampleFiles/upsert.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Twitter_Handle__c,Title
2+
pattest7,Guru

BulkAPISampleFiles/upsert.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Contact records -->
3+
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
4+
<sObject>
5+
<Twitter_Handle__c>pattest7</Twitter_Handle__c>
6+
<Title>Pirate</Title>
7+
</sObject>
8+
</sObjects>

BulkTK.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
BulkTK: Force.com Bulk API JavaScript Toolkit
2+
=============================================
3+
4+
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/).
5+
6+
Background
7+
==========
8+
9+
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.
10+
11+
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.
12+
13+
[bulk.page](https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/bulk.page) is a simple Visualforce single page application to demonstrate BulkTK. Try it out in a sandbox or developer edition.
14+
15+
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.
16+
17+
Dependencies
18+
============
19+
20+
* [jquery](http://jquery.com/)
21+
* [ForceTK](https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit)
22+
* [jxon](https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/jxon.js) (originally from the [Ratatosk](https://github.com/wireload/Ratatosk) project; this version preserves case in element and attribute names)
23+
24+
Example Usage
25+
=============
26+
27+
This example focuses on Visualforce. See the [ForceTK documentation](https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit) for details on authenticating from an external website, such as a Heroku app, or PhoneGap/Cordova.
28+
29+
First, include BulkTK and its dependencies:
30+
31+
<script src="{!$Resource.jquery}"></script>
32+
<script src="{!$Resource.forcetk}"></script>
33+
<script src="{!$Resource.jxon}"></script>
34+
<script src="{!$Resource.bulkTK}"></script>
35+
36+
Now create a ForceTK client:
37+
38+
var client = new forcetk.Client();
39+
client.setSessionToken('{!$Api.Session_ID}');
40+
41+
See the [ForceTK documentation](https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit) for details on authenticating from an external website, such as a Heroku app, or PhoneGap/Cordova.
42+
43+
Create a job
44+
------------
45+
46+
// See https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_reference_jobinfo.htm
47+
// for details of the JobInfo structure
48+
49+
// Insert Contact records in CSV format
50+
var job = {
51+
operation : 'insert',
52+
object : 'Contact',
53+
contentType : 'CSV'
54+
};
55+
56+
client.createJob(job, function(response) {
57+
jobId = response.jobInfo.id;
58+
console.log('Job created with id '+jobId+'\n');
59+
}, function(jqXHR, textStatus, errorThrown) {
60+
console.log('Error creating job', jqXHR.responseText);
61+
});
62+
63+
Add a batch of records to the job
64+
---------------------------------
65+
66+
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.
67+
68+
var csvData = "FirstName,LastName,Department,Birthdate,Description\n"+
69+
"Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast\n"+
70+
"Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."\n";
71+
72+
client.addBatch(jobId, "text/csv; charset=UTF-8", csvData,
73+
function(response){
74+
console.log('Added batch '+response.batchInfo.id+'. State: '+response.batchInfo.state+'\n');
75+
}, function(jqXHR, textStatus, errorThrown) {
76+
console.log('Error adding batch', jqXHR.responseText);
77+
});
78+
79+
See BulkAPISampleFiles for sample CSV and XML data for different operations.
80+
81+
Close the job
82+
-------------
83+
84+
You must close the job to inform Salesforce that no more batches will be submitted for the job.
85+
86+
client.closeJob(jobId, function(response){
87+
console.log('Job closed. State: '+response.jobInfo.state+'\n');
88+
}, function(jqXHR, textStatus, errorThrown) {
89+
console.log('Error closing job', jqXHR.responseText);
90+
});
91+
92+
Check batch status
93+
------------------
94+
95+
client.getBatchDetails(jobId, batchId, function(response){
96+
console.log('Batch state: '+response.batchInfo.state+'\n');
97+
}, function(jqXHR, textStatus, errorThrown) {
98+
console.log('Error getting batch details', jqXHR.responseText);
99+
});
100+
101+
Get batch results
102+
-----------------
103+
104+
Pass `true` as the `parseXML` parameter to get batch results for a query, false otherwise.
105+
106+
client.getBatchResult(jobId, batchId, false, function(response){
107+
console.log('Batch result: '+response);
108+
}, function(jqXHR, textStatus, errorThrown) {
109+
console.log('Error getting batch result', jqXHR.responseText);
110+
});
111+
112+
Bulk query
113+
----------
114+
115+
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.
116+
117+
var soql = 'SELECT Id, FirstName, LastName, Email FROM Contact';
118+
119+
client.addBatch(jobId, 'text/csv', soql, function(response){
120+
console.log('Batch state: '+response.batchInfo.state+'\n');
121+
}, function(jqXHR, textStatus, errorThrown) {
122+
console.log('Error getting batch result', jqXHR.responseText);
123+
});
124+
125+
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
126+
127+
client.getBatchResult(jobId, batchId, true, function(response){
128+
response['result-list'].result.forEach(function(resultId){
129+
client.getBulkQueryResult(jobId, batchId, resultId, function(response){
130+
console.log('Batch result: '+response);
131+
}, function(jqXHR, textStatus, errorThrown) {
132+
console.log('Error getting bulk query results', jqXHR.responseText);
133+
});
134+
});
135+
}, function(jqXHR, textStatus, errorThrown) {
136+
console.log('Error getting batch result', jqXHR.responseText);
137+
});

0 commit comments

Comments
 (0)