|
| 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 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 | +Example Usage |
| 18 | +============= |
| 19 | + |
| 20 | +You must create a ForceTK client first. On a Visualforce page, you would do: |
| 21 | + |
| 22 | + var client = new forcetk.Client(); |
| 23 | + client.setSessionToken('{!$Api.Session_ID}'); |
| 24 | + |
| 25 | +See the ForceTK documentation for authenticating from an external website, such as a Heroku app, or PhoneGap/Cordova. |
| 26 | + |
| 27 | +Create a job |
| 28 | +------------ |
| 29 | + |
| 30 | + // See https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_reference_jobinfo.htm |
| 31 | + // for details of the JobInfo structure |
| 32 | + |
| 33 | + // Insert Contact records in CSV format |
| 34 | + var job = { |
| 35 | + operation : 'insert', |
| 36 | + object : 'Contact', |
| 37 | + contentType : 'CSV' |
| 38 | + }; |
| 39 | + |
| 40 | + client.createJob(job, function(response) { |
| 41 | + jobId = response.jobInfo.id; |
| 42 | + console.log('Job created with id '+jobId+'\n'); |
| 43 | + }, function(jqXHR, textStatus, errorThrown) { |
| 44 | + console.log('Error creating job', jqXHR.responseText); |
| 45 | + }); |
| 46 | + |
| 47 | +Add a batch of records to the job |
| 48 | +--------------------------------- |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | + var csvData = "FirstName,LastName,Department,Birthdate,Description\n"+ |
| 53 | + "Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast\n"+ |
| 54 | + "Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."\n"; |
| 55 | + |
| 56 | + client.addBatch(jobId, "text/csv; charset=UTF-8", csvData, |
| 57 | + function(response){ |
| 58 | + console.log('Added batch '+response.batchInfo.id+'. State: '+response.batchInfo.state+'\n'); |
| 59 | + }, function(jqXHR, textStatus, errorThrown) { |
| 60 | + console.log('Error adding batch', jqXHR.responseText); |
| 61 | + }); |
| 62 | + |
| 63 | +See BulkAPISampleFiles for sample CSV and XML data for different operations. |
| 64 | + |
| 65 | +Close the job |
| 66 | +------------- |
| 67 | + |
| 68 | +You must close the job to inform Salesforce that no more batches will be submitted for the job. |
| 69 | + |
| 70 | + client.closeJob(jobId, function(response){ |
| 71 | + console.log('Job closed. State: '+response.jobInfo.state+'\n'); |
| 72 | + }, function(jqXHR, textStatus, errorThrown) { |
| 73 | + console.log('Error closing job', jqXHR.responseText); |
| 74 | + }); |
| 75 | + |
| 76 | +Check batch status |
| 77 | +------------------ |
| 78 | + |
| 79 | + client.getBatchDetails(jobId, batchId, function(response){ |
| 80 | + console.log('Batch state: '+response.batchInfo.state+'\n'); |
| 81 | + }, function(jqXHR, textStatus, errorThrown) { |
| 82 | + console.log('Error getting batch details', jqXHR.responseText); |
| 83 | + }); |
| 84 | + |
| 85 | +Get batch results |
| 86 | +----------------- |
| 87 | + |
| 88 | +Pass `true` as the `parseXML` parameter to get batch results for a query, false otherwise. |
| 89 | + |
| 90 | + client.getBatchResult(jobId, batchId, false, function(response){ |
| 91 | + console.log('Batch result: '+response); |
| 92 | + }, function(jqXHR, textStatus, errorThrown) { |
| 93 | + console.log('Error getting batch result', jqXHR.responseText); |
| 94 | + }); |
| 95 | + |
| 96 | +Bulk query |
| 97 | +---------- |
| 98 | + |
| 99 | +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. |
| 100 | + |
| 101 | + var soql = 'SELECT Id, FirstName, LastName, Email FROM Contact'; |
| 102 | + |
| 103 | + client.addBatch(jobId, 'text/csv', soql, function(response){ |
| 104 | + console.log('Batch state: '+response.batchInfo.state+'\n'); |
| 105 | + }, function(jqXHR, textStatus, errorThrown) { |
| 106 | + console.log('Error getting batch result', jqXHR.responseText); |
| 107 | + }); |
| 108 | + |
| 109 | +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 |
| 110 | + |
| 111 | + client.getBatchResult(jobId, batchId, true, function(response){ |
| 112 | + response['result-list'].result.forEach(function(resultId){ |
| 113 | + client.getBulkQueryResult(jobId, batchId, resultId, function(response){ |
| 114 | + console.log('Batch result: '+response); |
| 115 | + }, function(jqXHR, textStatus, errorThrown) { |
| 116 | + console.log('Error getting bulk query results', jqXHR.responseText); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }, function(jqXHR, textStatus, errorThrown) { |
| 120 | + console.log('Error getting batch result', jqXHR.responseText); |
| 121 | + }); |
0 commit comments