Skip to content

Instantly share code, notes, and snippets.

@IanVaughan
Created May 16, 2023 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanVaughan/d67e29cfee5c9300a1971d8e592970c0 to your computer and use it in GitHub Desktop.
Save IanVaughan/d67e29cfee5c9300a1971d8e592970c0 to your computer and use it in GitHub Desktop.
Matching SalesForce Leads
# frozen_string_literal: true
require 'csv'
require 'restforce'
CONFIG = {
username: '',
password: ENV['SFDC_PASSWORD'],
client_id: '',
client_secret: ENV['SFDC_CLIENT_SECRET'],
host: ''
}.freeze
Restforce.configure do |config|
config.username = CONFIG[:username]
config.password = CONFIG[:password]
config.host = CONFIG[:host]
config.client_id = CONFIG[:client_id]
config.client_secret = CONFIG[:client_secret]
config.api_version = '57.0'
end
client = Restforce.new
READ_FIELDS = %w[
Id Name Admin_Status__c Status IsConverted ConvertedAccountId
Email TAMI_Company_Email__c TAMI_Email_Address__c Generic_Email__c
Outfund_UID__c OutFund_Link__c
].freeze
output_csv = CSV.open('output.csv', 'wb')
output_csv << %w[company_id uid email app_id status] + READ_FIELDS
Thread.report_on_exception = true
threads = []
csv = CSV.read('company_users_applications.csv')
csv.shift
index = 0
total = csv.count
width = csv.count.to_s.chars.count
csv.map do |csv_id, csv_uid, csv_email, csv_app_id, csv_status|
sleep(0.1)
threads << Thread.new(csv_id, csv_uid, csv_email, csv_app_id, csv_status) do |id, uid, email, app_id, status|
index += 1
puts("[#{format("%#{width}d", index)}/#{total}] = #{email}")
output_csv << [id, uid, email, app_id, status] && next if email.nil? || email.empty?
encoded_email = email.gsub('-', '\-').gsub('+', '\+')
result = client.search("FIND {#{encoded_email}} RETURNING Lead (#{READ_FIELDS.join(',')})")
if result['searchRecords'].empty?
output_csv << [id, uid, email, app_id, status]
else
result['searchRecords'].map do |r|
output_csv << [id, uid, email, app_id, status] + r.values_at(*READ_FIELDS)
end.flatten
end
end
end
puts "Waiting for #{threads.count} threads to finish..."
threads.each(&:join)
output_csv.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment