Skip to content

Instantly share code, notes, and snippets.

@matt-newell
Last active February 14, 2022 14:59
Show Gist options
  • Select an option

  • Save matt-newell/8025398d0fdfcaa42d8be8377113a72b to your computer and use it in GitHub Desktop.

Select an option

Save matt-newell/8025398d0fdfcaa42d8be8377113a72b to your computer and use it in GitHub Desktop.
sfdc-bulk-upsert
#!/usr/bin/env node
const jsforce = require('jsforce');
const sfbulk = require('node-sf-bulk2');
const util = require('util');
const fs = require('fs');
const fsPromises = require('fs').promises;
const path = require('path');
const CFonts = require('cfonts')
async function submitBulkQueryJob() {
if (process.env.username && process.env.password) {
const output = CFonts.say('villagemd|', {
font: 'tiny',
gradient: ['yellow', 'blue'],
space: false
});
const conn = new jsforce.Connection({
loginUrl : 'https://test.salesforce.com',
accessToken: process.env.accessToken
});
//await conn.login(process.env.username, process.env.password);
const bulkconnect = {
'accessToken': conn.accessToken,
'apiVersion': '51.0',
'instanceUrl': process.env.instanceUrl
};
try {
const bulkapi2 = new sfbulk.BulkAPI2(bulkconnect);
const jobRequest = {
'object': process.argv[2].split('.')[0],
'externalIdFieldName': process.argv[2].split('.')[1],
'contentType': 'CSV',
'operation': 'upsert',
'lineEnding': 'LF'
};
const response = await bulkapi2.createDataUploadJob(jobRequest);
console.log('Creating bulk job')
if (response.id) {
// read csv data from the local file system
const data = await fsPromises.readFile(process.argv[3], "UTF-8")
const status = await bulkapi2.uploadJobData(response.contentUrl, data);
console.log('Uploading csv data')
if (status === 201) {
// close the job for processing
await bulkapi2.closeOrAbortJob(response.id, 'UploadComplete');
console.table(response)
}
} else{
console.log('err', response.data)
}
} catch (ex) {
console.log(JSON.stringify(ex, null, 2));
}
} else {
throw 'set environment variable with your orgs username and password'
}
}
submitBulkQueryJob()
{
"name": "sfdc-bulk",
"version": "1.0.0",
"description": "Bulk v2 > sfdc",
"main": "index.js",
"bin": {
"sfdc-bulk": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Matt Newell",
"license": "ISC",
"dependencies": {
"cfonts": "^2.10.0",
"fs": "0.0.1-security",
"jsforce": "^1.11.0",
"node-sf-bulk2": "0.0.23",
"path": "^0.12.7",
"util": "^0.12.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment