Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Last active May 17, 2023 16:50
Show Gist options
  • Save nfriend21/ac645dd422fc85e41dc839cbd0f831ca to your computer and use it in GitHub Desktop.
Save nfriend21/ac645dd422fc85e41dc839cbd0f831ca to your computer and use it in GitHub Desktop.
# The actual string of the start date and end date in the two lines below need to be adjusted to reflect the date range of the inquiries you want to update.
# This script is safe to run even on inquiries that successfully sync'd to Salesforce. Therefore, you dont need to find the exact Inquiries that didnt sync,
# you can just run this for the entire day where the problem occured.
start_date = Time.strptime('06-10-2020', '%m-%d-%Y').beginning_of_day.in_time_zone
end_date = Time.strptime('06-10-2020', '%m-%d-%Y').end_of_day.in_time_zone
errors = []
Inquiry.where('created_at >= ? AND created_at < ?', start_date, end_date).each do |i|
client = SalesforceConnector.client
lead_id = client.query("select Id, Email from Lead where Email = '#{i.email.delete(' ')}'").first.try(:Id) || 'x'
begin
sf_lead = client.find('Lead', lead_id)
if sf_lead["ConvertedOpportunityId"].present?
i.update_attributes(reconciled_in_salesforce: true)
else
begin
SalesforceLeadUpdater.fix_update_lead_from_inquiry(i)
i.update_attributes(reconciled_in_salesforce: true)
rescue => e
errors << [i.id, "ERROR 1======#{e}"]
end
end
rescue Faraday::Error::ResourceNotFound
begin
SalesforceLeadUpdater.create_lead(i.lead, i, nil, nil)
i.update_attributes(reconciled_in_salesforce: true)
rescue => e
errors << [i.id, "ERROR 2======#{e}"]
end
rescue => e
errors << [i.id, "ERROR 3======#{e}"]
end
end
@igor-railsmuffin
Copy link

igor-railsmuffin commented May 17, 2023

Script has few small errors:
instead of SalesforceLeadUpdater.fix_update_lead_from_inquiry(i) we need to use SalesforceLeadUpdater.new.fix_update_lead_from_inquiry(i)
instead of SalesforceLeadUpdater.create_lead(i.lead, i, nil, nil) we need to use SalesforceLeadUpdater.new.create_lead(i.lead, i, nil, nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment