Skip to content

Instantly share code, notes, and snippets.

@drwpow
Last active February 28, 2017 23:21
Show Gist options
  • Save drwpow/9402e9aed803fc32715b8cc41cb99b13 to your computer and use it in GitHub Desktop.
Save drwpow/9402e9aed803fc32715b8cc41cb99b13 to your computer and use it in GitHub Desktop.
# @contact = User contact information
# @project = Entire order, with budget, including multiple pieces
# Company Name if present; otherwise First Name + Last Name
company_name = @contact.company.present? ? @contact.company : "#{@contact.first_name} #{@contact.last_name}"
# 1. Create Account if not exists
account_id = salesforce.query("select Id from Account where Name = '#{j(company_name)}'")&.first&.Id
# 1a. Update if match
if account_id.present?
salesforce.update!('Account',
Id: account_id,
BillingCity: @contact.billing_city,
BillingPostalCode: @contact.billing_postal_code,
BillingState: @contact.billing_state,
BillingStreet: "#{@contact.billing_line1}\r\n#{@contact.billing_line2}",
ShippingCity: @contact.shipping_city,
ShippingPostalCode: @contact.shipping_postal_code,
ShippingState: @contact.shipping_state,
ShippingStreet: "#{@contact.shipping_line1}\r\n#{@contact.shipping_line2}",
Shipping_Address_1__c: @contact.shipping_line1,
Shipping_Address_2__c: @contact.shipping_line2,
Shipping_City__c: @contact.shipping_city,
Shipping_State__c: @contact.shipping_state,
Shipping_Zip_Code__c: @contact.shipping_postal_code,
)
# 1b. Create if no match
else
account_id = salesforce.create!('Account',
BillingCity: @contact.billing_city,
BillingPostalCode: @contact.billing_postal_code,
BillingState: @contact.billing_state,
BillingStreet: "#{@contact.billing_line1}\r\n#{@contact.billing_line2}",
Name: company_name,
ShippingCity: @contact.shipping_city,
ShippingPostalCode: @contact.shipping_postal_code,
ShippingState: @contact.shipping_state,
ShippingStreet: "#{@contact.shipping_line1}\r\n#{@contact.shipping_line2}",
Shipping_Address_1__c: @contact.shipping_line1,
Shipping_Address_2__c: @contact.shipping_line2,
Shipping_City__c: @contact.shipping_city,
Shipping_State__c: @contact.shipping_state,
Shipping_Zip_Code__c: @contact.shipping_postal_code,
)
end
# 2. Create Contact if not exists
contact_id = salesforce.query("select Id from Contact where AccountId = '#{account_id}' and FirstName = '#{j(@contact.first_name)}' and LastName = '#{j(@contact.last_name)}' and Email = '#{j(@contact.email)}'")&.first&.Id
# 2a. Update if match
if contact_id.present?
salesforce.update!('Contact',
Id: contact_id,
Email: @contact.email,
MailingCity: @contact.shipping_city,
MailingPostalCode: @contact.shipping_postal_code,
MailingState: @contact.shipping_state,
MailingStreet: "#{@contact.shipping_line1}\r\n#{@contact.shipping_line2}",
MobilePhone: @contact.phone,
)
# 2b. Create if no match
else
contact_id = salesforce.create!('Contact',
AccountId: account_id,
Email: @contact.email,
FirstName: @contact.first_name,
LastName: @contact.last_name,
MailingCity: @contact.shipping_city,
MailingPostalCode: @contact.shipping_postal_code,
MailingState: @contact.shipping_state,
MailingStreet: "#{@contact.shipping_line1}\r\n#{@contact.shipping_line2}",
MobilePhone: @contact.phone,
)
end
@contact.save
# 3. Create Opportunity__c
s3_folder = ""
if @artworks.present?
s3_folder = s3_folder_link(@artworks[0].image.url)
end
# 3a. Always create opportunity
opportunity_id = salesforce.create!('Opportunity__c',
'Account__c': account_id,
'Artwork_URL__c': s3_folder,
'Client_Provided_opportunity_value__c': @project.budget_with_cents,
'Contact__c': contact_id,
'Designer_Contact__c': @project.designer_name,
'Designer_Email__c': @project.designer_email,
'Opportunity_Source__c': 'Website',
'Product_Category__c': product_human(@project.pieces_json[0][:product]),
'Re_Order__c': @project.is_reorder,
'Re_Order_of_ID__c': @project.previous_order_number,
'Status__c': 'New',
Name: @project.name,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment