#Bulk v2 Query
env vars to setup export username='' export password=''+security_token
usage: npx https://gist.github.com/matt-newell/e8f016fb35c1ca714b52f00bb90c3b62 'Select Id From Account'
Results saved to result_JobId.log|csv
| #!/usr/bin/env node | |
| //console.log('v1', process.argv, process.cwd(), process.env); | |
| const jsforce = require('jsforce'); | |
| const sfbulk = require('node-sf-bulk2'); | |
| const util = require('util'); | |
| const fs = require('fs'); | |
| const fsPromises = require('fs').promises | |
| const CFonts = require('cfonts') | |
| async function submitBulkQueryJob() { | |
| const start = new Date() | |
| 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': conn.instanceUrl | |
| }; | |
| console.log('βοΈ -> Connecting to Salesforce ' + conn.instanceUrl + '\n') | |
| try { | |
| const bulkapi2 = new sfbulk.BulkAPI2(bulkconnect); | |
| const queryInput = { | |
| 'query': process.argv[2], | |
| 'operation': 'query' | |
| }; | |
| var response = await bulkapi2.submitBulkQueryJob(queryInput) | |
| console.log('πΌ -> Bulk v2 job created\n') | |
| console.log('β± -> Waiting for job to complete\n') | |
| while(response.state !== 'JobComplete'){ | |
| response = await bulkapi2.getBulkQueryJobInfo(response.id) | |
| } | |
| const jobQuery = { | |
| 'jobId': response.id | |
| } | |
| console.log(`π -> query: ${process.argv[2]}\n`) | |
| console.table(response) | |
| await fsPromises.writeFile('./' + response.object + response.id + '.log', `query: ${process.argv[2]}\n`) | |
| await fsPromises.appendFile('./' + response.object + response.id + '.log', JSON.stringify(response, null, 4)) | |
| var result = await bulkapi2.getBulkQueryResults(response.id) | |
| await fsPromises.writeFile('./' + response.object + response.id + '.csv', result.data) | |
| await fsPromises.appendFile('./' + response.object + response.id + '.log', '\n./' + response.object + response.id) | |
| var conti = JSON.parse(JSON.stringify(result.headers)) | |
| console.log('π -> Writing ' + response.object + ' data...\n') | |
| while(conti['sforce-locator'] != 'null'){ | |
| console.log('π Writing data...\n') | |
| result = await bulkapi2.getBulkQueryResults(response.id, conti['sforce-locator']) | |
| conti = JSON.parse(JSON.stringify(result.headers)) | |
| await fsPromises.writeFile('./' + response.object + '_' + conti['sforce-locator'] + '_' + response.id + '.csv', result.data) | |
| await fsPromises.appendFile('./' + response.object + response.id + '.log', '\n./' + response.object + '_' + conti['sforce-locator'] + '_' + response.id ) | |
| } | |
| console.log('β Dude its done π\n') | |
| const stop = new Date() | |
| console.log(`β± -> ${(start - stop)/1000} seconds\n`) | |
| } catch (ex) { | |
| console.log(ex); | |
| } | |
| } 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": { | |
| "child_process": "^1.0.2", | |
| "cfonts": "^2.10.0", | |
| "exec": "^0.2.1", | |
| "fs": "0.0.1-security", | |
| "jsforce": "^1.11.0", | |
| "node-sf-bulk2": "0.0.23", | |
| "path": "^0.12.7", | |
| "util": "^0.12.4" | |
| } | |
| } |