Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Created July 22, 2022 22:05
Show Gist options
  • Save nfriend21/58e6c0179ed4a0116f845d935239c093 to your computer and use it in GitHub Desktop.
Save nfriend21/58e6c0179ed4a0116f845d935239c093 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('05-11-2021', '%m-%d-%Y').beginning_of_day.in_time_zone
end_date = Time.strptime('05-12-2021', '%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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment