Last active
August 29, 2015 14:03
-
-
Save martintajur/d231a6fe54f795f8370c to your computer and use it in GitHub Desktop.
Pipedrive people phone fields merger script (it can be used to combine multiple phone field values inside the single, multi-valued phone field)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Pipedrive people phone fields merger script | |
It can be used to combine existing, multiple separate phone field values inside the single, multi-valued phone field. | |
MIT Licence. | |
*/ | |
var P = require('pipedrive'); | |
var _ = require('lodash'); | |
var p = new P.Client('PUT_YOUR_API_TOKEN_HERE'); | |
var separatePhoneFields = { | |
'eed60a8c65843b3ff0ccc235553269b9c871bed7': 'home', | |
'f5d394da80ace346148e92303a504d837d4981fe': 'work', | |
'ffbd12e2c2a674ec7f7f4d67d4b9c1162c9c554d': 'mobile', | |
'5a23f51baf1b5c530d3b85264b9492e2371d697f': 'other' | |
}; | |
console.log('Fetching all people... this may take a while. Please stand by.'); | |
var i = 0; | |
// fetch all people from the pipedrive account: | |
p.getAll('Persons', function(err, persons) { | |
if (err) { | |
throw err; | |
} | |
console.log('Fetched '+persons.length+' in total, proceeding with phone numbers merge...'); | |
_.each(persons, function(person) { | |
// first grab whatever we have in the phone field for people already: | |
var combinedPhoneField = person.get('phone'); | |
var oldField = _.clone(person.get('phone')); | |
// if we get back a string (a single phone number), we should form an array to start pushing more values to it: | |
if (_.isString(combinedPhoneField)) { | |
combinedPhoneField = [{ value: person.phone, label: 'work', primary: true }]; | |
} | |
// if we still don't have an array set for the combinedPhoneField, lets form an empty array to begin with: | |
if (!_.isArray(combinedPhoneField) || (_.first(combinedPhoneField) || _.isEmpty(_.first(combinedPhoneField).value))) { | |
combinedPhoneField = []; | |
} | |
// iterate through other (custom) phone fields, pushing the values into the single phone field: | |
_.each(_.keys(separatePhoneFields), function(otherPhoneField) { | |
if (!_.isEmpty(person.get(otherPhoneField))) { | |
combinedPhoneField.push({ value: person.get(otherPhoneField), label: separatePhoneFields[otherPhoneField], primary: combinedPhoneField.length === 0 }); | |
} | |
}); | |
// set the new 'phone' value to the person: | |
person.set('phone', combinedPhoneField); | |
// save the person with the new data to Pipedrive API: | |
person.save(function(err) { | |
if (err) { | |
console.log('Error occurred while saving person '+person.name+' (id: '+person.id+')! ' + err); | |
return; | |
} | |
i++; | |
console.log('Person '+person.name+' (id: '+person.id+') saved! ('+i+' of '+persons.length+' done)...\nold phone field:'+JSON.stringify(oldField)+'\nnew phone field:'+JSON.stringify(combinedPhoneField)+'\n----'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment