Last active
December 25, 2015 15:49
-
-
Save rsobers/7001660 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rest-client' | |
require 'csv' | |
require 'json' | |
# Have you ever accidentally changed a property for a large group of contacts via an import or workflow? | |
# This script allows you to take that list of contacts and revert the affected property to its most recent previous value. | |
# When finished, you'll have a CSV file with the previous values for each of the contacts in your list. Simply import that | |
# list into HubSpot and your property values will be reverted. | |
path = './contacts-to-revert.csv' | |
@@hapi_key = 'insert-your-hapi-key-here' | |
tot_count = 0 | |
fix_count = 0 | |
output = File.open( "./import-file.csv", "a") | |
CSV.foreach(path, :quote_char => '"', :col_sep => ',', :row_sep => :auto) do |row| | |
tot_count +=1 | |
e = row[0] | |
begin | |
response = RestClient.get "api.hubapi.com/contacts/v1/contact/email/#{e}/profile?hapikey=#{@@hapi_key}" | |
parsed = JSON.parse(response.body) | |
versions = parsed['properties']['leadtype']['versions'] | |
if versions.count > 1 then | |
current_value = versions[0]['value'] | |
previous_value = versions[1]['value'] | |
fix_count +=1 | |
output.write(e + ", " + previous_value) | |
output.write("\n") | |
end | |
STDOUT.write "\r#{fix_count} / #{tot_count}" | |
STDOUT.flush | |
rescue Exception => e | |
p e.message | |
end | |
end | |
output.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment