Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created June 8, 2015 15:43
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 tstachl/a4a86d31eda120abc849 to your computer and use it in GitHub Desktop.
Save tstachl/a4a86d31eda120abc849 to your computer and use it in GitHub Desktop.
Create an outbound email case for a customer by email.
require 'desk_api' # @see https://github.com/forcedotcom/salesforce-deskcom-api
# authenticate `DeskApi` with your Desk.com instance
#####
# FIND OR CREATE CUSTOMER
#####
# you should get the customer data from your system
customer_data = {
first_name: 'John',
last_name: 'Smith',
emails: [{ type: 'home', value: 'jsmith@example.com' }]
}
# find the customer by email address
query = customer_data.emails.map { |x| x[:value] }.join(',')
search = DeskApi.customers.search(email: query)
if search.total_entries == 0 # create customer if he doesn't exist
customer = DeskApi.customers.create(customer_data)
else # use the existing customer
customer = search.entries.first
end
#####
# CREATE THE OUTBOUND CASE
#####
# this is the email you want to send to your customer
email_data = {
direction: 'out',
status: 'pending',
subject: 'Email Subject',
body: 'Email Message',
from: 'Laura Agent <lagent@example.com>',
to: 'John Smith <jsmith@example.com>',
cc: 'Mark Snider <msnider@example.com>',
bcc: 'Reid Wheeler <rwheeler@example.com>'
}
# additional ticket information
ticket_data = {
type: 'email',
subject: email_data[:subject],
priority: 4,
status: 'pending',
labels: ['Follow Up'],
message: email_data,
custom_fields: { level: 'vip' },
_links: {
customer: customer.get_self,
assigned_user: { href: '/api/v2/users/1', class: 'user' },
assigned_group: { href: '/api/v2/groups/1', class: 'group' }
}
}
# create the case
ticket = DeskApi.cases.create(ticket_data)
# at this point the case has been created
# and the email is queued to be sent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment