Permalink
Browse files

added several examples

  • Loading branch information...
1 parent 1f724c0 commit 26ae5ddebd26f73d1edf1e4c02704147c00b5fd6 @stomita committed Jan 11, 2013
Showing with 146 additions and 0 deletions.
  1. +77 −0 examples/migration.js
  2. +69 −0 examples/pipeline.js
View
@@ -0,0 +1,77 @@
+var async = require('async');
+var sf = require('../lib/salesforce');
+
+var config = {};// { logLevel: "DEBUG" };
+var conn1 = new sf.Connection(config);
+var conn2 = new sf.Connection(config);
+
+async.waterfall([
+ function(next) {
+ async.parallel([
+ function(cb) {
+ conn1.login(process.env.SF_USERNAME_1, process.env.SF_PASSWORD_1, cb);
+ },
+ function(cb) {
+ conn2.login(process.env.SF_USERNAME_2, process.env.SF_PASSWORD_2, cb);
+ }
+ ], next);
+ },
+ function(rets, next) {
+ conn2.sobject('Account').count(next);
+ },
+ function(cnt, next) {
+ console.log("Account count in conn2 : " + cnt);
+ async.parallel([
+ function(cb) {
+ conn1.sobject('Account').describe(cb);
+ },
+ function(cb) {
+ conn2.sobject('Account').describe(cb);
+ }
+ ], next);
+ },
+ function(sobjects, next) {
+ var so1 = sobjects[0], so2 = sobjects[1];
+ var fields1 = {};
+ so1.fields.forEach(function(f) { fields1[f.name] = 1; });
+ var fields2 = {};
+ so2.fields.forEach(function(f) {
+ if (fields1[f.name] && f.updateable && !f.custom && f.type !== 'reference') {
+ fields2[f.name] = 1;
+ }
+ });
+
+ conn1.sobject('Account').find({}, fields2)
+ .pipe(conn2.bulk.load('Account', 'insert'))
+ .on('response', function(res) { next(null, res); })
+ .on('error', function(err){ next(err); });
+ },
+ function(rets, next) {
+ var success = rets.filter(function(r) { return r.success; }).length;
+ var failure = rets.length - success;
+ console.log("bulkload sucess = " + success + ", failure = " + failure);
+ conn2.sobject('Account').count(next);
+ },
+ function(cnt, next) {
+ console.log("Account count in conn2 : " + cnt);
+ conn2.sobject('Account').find({ CreatedDate : sf.Date.TODAY }).exec(next);
+ },
+ function(records, next) {
+ console.log("deleting created records ("+records.length+")");
+ conn2.bulk.load('Account', 'delete', records, next);
+ },
+ function(rets, next) {
+ var success = rets.filter(function(r) { return r.success; }).length;
+ var failure = rets.length - success;
+ console.log("delete sucess = " + success + ", failure = " + failure);
+ conn2.sobject('Account').count(next);
+ },
+ function(cnt, next) {
+ console.log("Account count in conn2 : " + cnt);
+ next();
+ }
+], function(err, res) {
+ if (err) {
+ console.error(err);
+ }
+});
View
@@ -0,0 +1,69 @@
+var fs = require('fs');
+var async = require('async');
+var sf = require('../lib/salesforce');
+
+var config = {};// { logLevel: "DEBUG" };
+var conn = new sf.Connection(config);
+
+var Opportunity = conn.sobject('Opportunity');
+
+async.waterfall([
+ function(next) {
+ conn.login(process.env.SF_USERNAME, process.env.SF_PASSWORD, next);
+ },
+ function(sobjects, next) {
+ Opportunity.find({ AccountId: { $ne: null }}, { Id: 1, Name: 1, "Account.Name": 1 })
+ .pipe(sf.RecordStream.map(function(r) {
+ r.Name = r.Account.Name + ' *** ' + r.Name;
+ return r;
+ }))
+ .pipe(Opportunity.updateBulk())
+ .on('response', function(rets) {
+ next(null, rets);
+ })
+ .on('error', function(err) {
+ next(err);
+ });
+
+ },
+ function(rets, next) {
+ var success = rets.filter(function(r) { return r.success; }).length;
+ var failure = rets.length - success;
+ console.log("bulk update sucess = " + success + ", failure = " + failure);
+ next();
+ },
+ function(next) {
+ Opportunity.find({ Name : { $like: '% *** %' }}, { Id: 1, Name: 1 })
+ .pipe(sf.RecordStream.map(function(r) {
+ r.Name = r.Name.replace(/^.+ \*\*\* /g, '');
+ return r;
+ }))
+ .pipe(Opportunity.updateBulk())
+ .on('response', function(rets) {
+ next(null, rets);
+ })
+ .on('error', function(err) {
+ next(err);
+ });
+ },
+ function(rets, next) {
+ var success = rets.filter(function(r) { return r.success; }).length;
+ var failure = rets.length - success;
+ console.log("bulk update sucess = " + success + ", failure = " + failure);
+ next();
+ },
+ function(next) {
+ Opportunity
+ .find({}, { Id: 1, Name: 1, Amount: 1, StageName: 1, CreatedDate: 1 })
+ .pipe(sf.RecordStream.filter(function(r) {
+ return r.Amount > 500000;
+ }))
+ .stream().pipe(fs.createWriteStream("opps.csv"))
+ .on('end', function() { next(); })
+ .on('error', function(err) { next(err); });
+ }
+], function(err, res) {
+ if (err) {
+ console.error(err);
+ }
+});

0 comments on commit 26ae5dd

Please sign in to comment.