Api - Problem to set property hs_email_headers on incoming Email Object
Hey @AJoshi95, thank you for posting in our Community.
Could you please specify what you are currently experiencing? Since this is not a new post, the more information, screenshots, and details you can provide, the better I can advise on the next steps.
I am using Ruby gem hubspot-api-client for my integration. (I am pretty new to Hubspot integration as well as Ruby and Ruby on Rails.)
Below is a code snippet where I am updating the Contact, Company and Email objects. The Contact, Company and Email objects get created successfully, all the associations also get created. The only issue is the Email headers not showing correctly.
def save_to_crm
contact_association = []
if company_name?
company_body = {associations:[], properties:{"name":company_name, "domain": company_website}}
company_api_response = crm_client.crm.companies.basic_api.create(body: company_body)
if company_api_response.id
contact_association = [{"types":[{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId":1}], "to":{"id":company_api_response.id}}]
end
end
contact_body = {associations:contact_association, properties:{"firstname": first_name, "lastname": last_name, "email": email_address, "company": company_name}}
contact_api_response = crm_client.crm.contacts.basic_api.create(body: contact_body)
if contact_api_response.id
email_association = [{"types":[{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId":198}], "to":{"id":contact_api_response.id}}]
if company_api_response.id
email_association[1] = {"types":[{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId":186}], "to":{"id":company_api_response.id}}
end
email_header_hash = {"from":{"email":email_address, "firstName":first_name, "lastName":last_name}, "sender":{"email":email_address, "firstName":first_name,"lastName":last_name},"to":{"email":"info@mydomain.com", "firstName":"Info", "lastName":"MyDomain.com" }, "cc":[], "bcc":[]}
email_body = {associations: email_association, properties:{"hs_email_direction": "INCOMING_EMAIL", "hs_email_subject": "Website Contact Us Page - Contact", "hs_email_text": message, "hs_timestamp": Time.now.utc.iso8601, "hs_email_headers": email_header_hash.to_json}}
email_api_response = crm_client.crm.objects.emails.basic_api.create(body: email_body)
end
end
def crm_client
return Hubspot::Client.new(access_token: Rails.application.credentials.dig(:hubspot, :API_KEY))
end
The form used to capture the data:
Resulting Company record:
Notice the data highlighted in yellow.
Company, Contact and associations updated correctly but the email header details are blank.