Skip to content

Instantly share code, notes, and snippets.

@tarot
Last active August 29, 2015 14:12
Show Gist options
  • Save tarot/48a1e5e9d0b705786fe4 to your computer and use it in GitHub Desktop.
Save tarot/48a1e5e9d0b705786fe4 to your computer and use it in GitHub Desktop.
jsforceを使ってデータパッチするサンプル。そのままだとContact.LastName = Contact.LastName + Contact.Nameになっちゃうよ!
jsforce = require 'jsforce'
Promise = require 'bluebird'
streamify = require 'stream-array'
csv = require 'csv'
fs = require 'fs'
program = require 'commander'
program
.option '-d, --deploy', 'deploy'
.parse process.argv
require('dotenv').load()
username = process.env.SALESFORCE_USERNAME
password = process.env.SALESFORCE_PASSWORD
host = process.env.SALESFORCE_HOST || 'login.salesforce.com'
host = "https://#{host}" unless host.match(/^https:\/\//)
connection = new jsforce.Connection loginUrl: host
connection.login username, password
.then ->
new Promise (resolve, reject) ->
records = []
connection.bulk.query('select Id, Name, LastName from Contact').stream()
.pipe csv.parse(columns: true)
.on 'data', (row) -> records.push row
.on 'error', -> reject arguments
.on 'end', -> resolve records
.then (records) ->
# データを加工してreadonly項目を除去
records.forEach (e, i) ->
e.LastName = e.LastName + e.Name
delete e.Name
unless program.deploy
streamify records
.pipe csv.stringify(header: true)
.pipe fs.createWriteStream "./dryrun-#{new Date().toISOString()}.csv"
return
connection.sobject('Contact').updateBulk records
.then (results) ->
return unless program.deploy
streamify results.map (e) ->
id: e.id, success: String(e.success), errors: e.errors.join('\n')
.pipe csv.stringify(header: true)
.pipe fs.createWriteStream "./results-#{new Date().toISOString()}.csv"
error_count = results.filter((e) -> !e.success).length
console.error "#{error_count} errors" if error_count > 0
.fail ->
console.error arguments
{
"name": "datapatch_sample",
"dependencies": {
"bluebird": "^2.4.2",
"commander": "^2.5.1",
"csv": "^0.4.1",
"dotenv": "^0.4.0",
"jsforce": "^1.3.1",
"stream-array": "^1.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment