Skip to content

Instantly share code, notes, and snippets.

@metadaddy
Created Aug 31, 2015
Embed
What would you like to do?
Get job/batch status correctly with BulkTK
<apex:page docType="html-5.0" title="Bulk Uploader" sidebar="false">
<div id="byte_content"></div>
<input type="file" id="files" onchange="upload()"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="{!$Resource.forcetk}"></script>
<script src="{!$Resource.jxon}"></script>
<script src="{!$Resource.bulkTK}"></script>
<script src="{!$Resource.vkbeautify}"></script>
<script>
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
var needToInsertRecord= new Array();
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
//document.getElementById('byte_content').textContent = evt.target.result;
var data = evt.target.result;
var client = new forcetk.Client();
client.setSessionToken('{!$Api.Session_ID}');
console.log('starting step 1');
client.createJob({
operation : 'insert',
object : 'Account',
contentType : 'CSV'
}, function(response) {
var csvData = data;
//console.log(csvData);
jobId = response.jobInfo.id;
console.log('Job created with id '+jobId+'\n');
console.log('data size = '+csvData.length);
client.addBatch(jobId, "text/csv; charset=UTF-8", csvData, function(response){
console.log('Adding batch');
console.log('Added batch '+response.batchInfo.id+'. State: '+response.batchInfo.state+'\n');
batchId = response.batchInfo.id;
client.getBatchDetails(jobId, batchId, function(response){
console.log('Batch state: '+response.batchInfo.state+'\n');
console.log('Batch size: '+response.batchInfo.size+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error getting batch details', jqXHR.responseText);
});
client.closeJob(jobId, function(response){
console.log('In job close function');
getJobDetails(client, jobId);
//console.log('Job closed State '+response.jobInfo.state+'\n');
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error closing job', jqXHR.responseText);
});
}, function(jqXHR, textStatus, errorThrown) {
console.log('Error adding batch', jqXHR.responseText);
});
});
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
// Set up vars so getJobDetails works!
var batches = 1; // Just a single batch in this example
var operation = 'insert';
var contentType = 'CSV';
function getJobDetails(client, jobId){
client.getJobDetails(jobId, function(response){
console.log(response.jobInfo.numberRecordsProcessed+' records processed\n');
if ((response.jobInfo.numberBatchesCompleted +
response.jobInfo.numberBatchesFailed) === batches) {
console.log('Done!\n');
client.getJobBatchDetails(jobId, function(response){
response.batchInfoList.batchInfo.forEach(function(batch){
var batchId = batch.id;
client.getBatchResult(jobId, batchId, (operation === 'query'), function(response){
console.log('Batch result:\n');
if (operation === 'query') {
response['result-list'].result.forEach(function(result){
client.getBulkQueryResult(jobId, batchId, result, function(response){
console.log('Query result:\n');
var text = (contentType === 'CSV') ? response : vkbeautify.xml(response);
console.log(text);
}, function(jqXHR, textStatus, errorThrown){
console.log('Error getBulkQueryResult', jqXHR.responseText);
});
});
} else {
var text = (contentType === 'CSV') ? response : vkbeautify.xml(response);
console.log(text);
}
}, function(jqXHR, textStatus, errorThrown){
console.log('Error getBatchResult', jqXHR.responseText);
});
});
}, function(jqXHR, textStatus, errorThrown){
console.log('Error getJobBatchDetails', jqXHR.responseText);
});
} else {
setTimeout(function(){
getJobDetails(client, jobId);
}, 1000);
}
}, function(jqXHR, textStatus, errorThrown){
console.log('Error getJobDetails', jqXHR.responseText);
});
}
function upload() {
readBlob();
}
</script>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment