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

Commit 29176db

Browse files
author
Pat Patterson
committed
Refactored BulkTK for promises.
1 parent ded55d6 commit 29176db

File tree

4 files changed

+190
-141
lines changed

4 files changed

+190
-141
lines changed

BulkAPISampleFiles/update.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- Contact records -->
33
<sObjects xmlns="http://www.force.com/2009/06/asyncapi/dataload">
44
<sObject>
5-
<Id>003E000001CbzBi</Id>
5+
<Id>003E000001VB8F6IAL</Id>
66
<Title>Pirate</Title>
77
</sObject>
88
</sObjects>

BulkTK.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ Create a job
5353
contentType : 'CSV'
5454
};
5555

56-
client.createJob(job, function(response) {
56+
client.createJob(job)
57+
.then(function(response) {
5758
jobId = response.jobInfo.id;
5859
console.log('Job created with id '+jobId+'\n');
59-
}, function(jqXHR, textStatus, errorThrown) {
60+
})
61+
.catch(function(jqXHR, textStatus, errorThrown) {
6062
console.log('Error creating job', jqXHR.responseText);
6163
});
6264

@@ -69,10 +71,11 @@ You can add multiple batches to the job; each batch can contain up to 10,000 rec
6971
"Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast\n"+
7072
"Ian,Dury,R&D,,"World-renowned expert in fuzzy logic design. Influential in technology purchases."\n";
7173

72-
client.addBatch(jobId, "text/csv; charset=UTF-8", csvData,
73-
function(response){
74+
client.addBatch(jobId, "text/csv; charset=UTF-8", csvData)
75+
.then(function(response){
7476
console.log('Added batch '+response.batchInfo.id+'. State: '+response.batchInfo.state+'\n');
75-
}, function(jqXHR, textStatus, errorThrown) {
77+
})
78+
.catch(function(jqXHR, textStatus, errorThrown) {
7679
console.log('Error adding batch', jqXHR.responseText);
7780
});
7881

@@ -83,18 +86,22 @@ Close the job
8386

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

86-
client.closeJob(jobId, function(response){
89+
client.closeJob(jobId)
90+
.then(function(response){
8791
console.log('Job closed. State: '+response.jobInfo.state+'\n');
88-
}, function(jqXHR, textStatus, errorThrown) {
92+
})
93+
.catch(function(jqXHR, textStatus, errorThrown) {
8994
console.log('Error closing job', jqXHR.responseText);
9095
});
9196

9297
Check batch status
9398
------------------
9499

95-
client.getBatchDetails(jobId, batchId, function(response){
100+
client.getBatchDetails(jobId, batchId)
101+
.then(function(response){
96102
console.log('Batch state: '+response.batchInfo.state+'\n');
97-
}, function(jqXHR, textStatus, errorThrown) {
103+
})
104+
.catch(function(jqXHR, textStatus, errorThrown) {
98105
console.log('Error getting batch details', jqXHR.responseText);
99106
});
100107

@@ -103,9 +110,10 @@ Get batch results
103110

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

106-
client.getBatchResult(jobId, batchId, false, function(response){
113+
client.getBatchResult(jobId, batchId, false)
114+
.then(function(response){
107115
console.log('Batch result: '+response);
108-
}, function(jqXHR, textStatus, errorThrown) {
116+
}).then(function(jqXHR, textStatus, errorThrown) {
109117
console.log('Error getting batch result', jqXHR.responseText);
110118
});
111119

@@ -116,22 +124,28 @@ When adding a batch to a bulk query job, the `contentType` for the request must
116124

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

119-
client.addBatch(jobId, 'text/csv', soql, function(response){
127+
client.addBatch(jobId, 'text/csv', soql)
128+
.then(function(response){
120129
console.log('Batch state: '+response.batchInfo.state+'\n');
121-
}, function(jqXHR, textStatus, errorThrown) {
130+
})
131+
.catch(function(jqXHR, textStatus, errorThrown) {
122132
console.log('Error getting batch result', jqXHR.responseText);
123133
});
124134

125135
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
126136

127-
client.getBatchResult(jobId, batchId, true, function(response){
137+
client.getBatchResult(jobId, batchId, true)
138+
.then(function(response){
128139
response['result-list'].result.forEach(function(resultId){
129-
client.getBulkQueryResult(jobId, batchId, resultId, function(response){
140+
client.getBulkQueryResult(jobId, batchId, resultId)
141+
.then(function(response){
130142
console.log('Batch result: '+response);
131-
}, function(jqXHR, textStatus, errorThrown) {
143+
})
144+
.catch(function(jqXHR, textStatus, errorThrown) {
132145
console.log('Error getting bulk query results', jqXHR.responseText);
133146
});
134147
});
135-
}, function(jqXHR, textStatus, errorThrown) {
148+
})
149+
.catch(function(jqXHR, textStatus, errorThrown) {
136150
console.log('Error getting batch result', jqXHR.responseText);
137151
});

bulk.page

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,17 @@ Sample Visualforce page for Force.com Bulk API JavaScript Toolkit
148148
}
149149
job.contentType = contentType;
150150

151-
client.createJob(job, function(response) {
151+
client.createJob(job)
152+
.then(function(response) {
152153
batches = 0;
153154
jobId = response.jobInfo.id;
154155
$message.text('Job created with id '+jobId+'\n');
155156
$("."+$operation.val()).show();
156157
if (operation === 'query') {
157158
$soql.val("SELECT Id FROM "+object);
158159
}
159-
}, showError);
160+
})
161+
.catch(showError);
160162
}
161163

162164
function setQuery() {
@@ -165,47 +167,57 @@ Sample Visualforce page for Force.com Bulk API JavaScript Toolkit
165167
? "text/csv; charset=UTF-8"
166168
: "application/xml; charset=UTF-8";
167169

168-
client.addBatch(jobId, mimeType, soql, function(response){
170+
client.addBatch(jobId, mimeType, soql)
171+
.then(function(response){
169172
batches++;
170173
$message.append('Batch state: ',response.batchInfo.state+'\n');
171174
$close.show();
172-
}, showError);
175+
})
176+
.catch(showError);
173177
}
174178

175179
function getJobDetails(client, jobId){
176-
client.getJobDetails(jobId, function(response){
180+
client.getJobDetails(jobId)
181+
.then(function(response){
177182
$message.append(response.jobInfo.numberRecordsProcessed+' records processed\n');
178183
if ((response.jobInfo.numberBatchesCompleted +
179184
response.jobInfo.numberBatchesFailed) === batches) {
180185
$message.append('Done!\n');
181-
client.getJobBatchDetails(jobId, function(response){
186+
client.getJobBatchDetails(jobId)
187+
.then(function(response){
182188
response.batchInfoList.batchInfo.forEach(function(batch){
183189
var batchId = batch.id;
184-
client.getBatchResult(jobId, batchId, (operation === 'query'), function(response){
190+
client.getBatchResult(jobId, batchId, (operation === 'query'))
191+
.then(function(response){
185192
$message.append('Batch result:\n');
186193
if (operation === 'query') {
187194
response['result-list'].result.forEach(function(result){
188-
client.getBulkQueryResult(jobId, batchId, result, function(response){
195+
client.getBulkQueryResult(jobId, batchId, result)
196+
.then(function(response){
189197
$message.append('Query result:\n');
190198
var text = (contentType === 'CSV') ? response : vkbeautify.xml(response);
191199
$message.append(document.createTextNode(text));
192200
reset();
193-
}, showError);
201+
})
202+
.catch(showError);
194203
});
195204
} else {
196205
var text = (contentType === 'CSV') ? response : vkbeautify.xml(response);
197206
$message.append(document.createTextNode(text));
198207
reset();
199208
}
200-
}, showError);
209+
})
210+
.catch(showError);
201211
});
202-
}, showError);
212+
}).
213+
catch(showError);
203214
} else {
204215
setTimeout(function(){
205216
getJobDetails(client, jobId);
206217
}, 1000);
207218
}
208-
}, showError);
219+
})
220+
.catch(showError);
209221
}
210222

211223
function upload() {
@@ -215,29 +227,34 @@ Sample Visualforce page for Force.com Bulk API JavaScript Toolkit
215227
? "text/csv; charset=UTF-8"
216228
: "application/xml; charset=UTF-8";
217229

218-
client.addBatch(jobId, mimeType,
219-
file, function(response){
230+
client.addBatch(jobId, mimeType, file)
231+
.then(function(response){
220232
batches++;
221233
$message.append('Batch state: ',response.batchInfo.state+'\n');
222234
$close.show();
223-
}, showError);
235+
})
236+
.catch(showError);
224237
}
225238

226239
function closeJob() {
227-
client.closeJob(jobId, function(response){
240+
client.closeJob(jobId)
241+
.then(function(response){
228242
$message.append('Job state: '+response.jobInfo.state+'\n');
229243
getJobDetails(client, jobId);
230-
}, showError);
244+
})
245+
.catch(showError);
231246
}
232247

233248
$(document).ready(function(){
234-
client.describeGlobal(function(response){
249+
client.describeGlobal()
250+
.then(function(response){
235251
$object.empty();
236252
response.sobjects.forEach(function(sobject){
237253
$object.append('<option value='+sobject.name+'>'+sobject.label+'</option>');
238254
});
239255
$object.val('Contact');
240-
}, showError);
256+
})
257+
.catch(showError);
241258

242259
$message = $("#message");
243260
$operation = $("#operation");

0 commit comments

Comments
 (0)