Problem Associating Email with Ticket

I integrated hubspot api to custom help form within our app. it successfully creating contacts, tickets, associate contact with ticket, but throw error when trying to associate email with ticket.

Here is code I am using for email association

var emailData = new
{
 properties = new
 {
 hs_timestamp = DateTime.UtcNow.ToString("o"), // ISO 8601 format
 hs_email_subject = "Retry 02 - Resolving Ticket Issue",
 hs_email_html = "<p>Ignore - Working on Tickes -> Contacts -> Email association in Hubspot.</p>",
 hs_email_status = "SENT",
 hs_email_direction = "EMAIL" // Use "EMAIL" for outgoing emails
 },
 associations = new[]
 {
 new
 {
 to = new
 {
 id = "21479416916", // Ticket ID
 },
 types = new[]
 {
 new
 {
 associationCategory = "HUBSPOT_DEFINED",
 associationTypeId = 26 // 26 is the association type ID for email to ticket
 }
 }
 }
 }
};

 await HubSpotHelper.SendEmailAsync(emailData);

 // sending post requests to
 https://api.hubapi.com/crm/v3/objects/emails

In result get the following error

{
 "status": "error",
 "message": "invalid from object type 0-49 for associations to be created. expected: 0-5",
 "correlationId": "029ffd0d-0356-487c-9052-43b8a2c7d152",
 "category": "VALIDATION_ERROR"
}

Can someone help how to correctly associate email with ticket / contact via hubspot api.

Hey @MWebb5, thank you for posting in our Community!

First, ensure the email record is successfully created by retrieving it via the API. If it exists, update your request to correctly reference the email as the “from” object before associating it with the ticket.

To our top experts, @Anton and @RSchweighart do you have any recommendations for @MWebb5 matter?

Thank you,

Pam

The main issue i am facing creating email when I created new ticket. I am using engagement API but it doesn’t work Accounts Dashboard | HubSpot

Can guide some reference code who to create email and then later associate with ticket.

Hi @MWebb5,

I recommend making a PUT request to associate the ticket to the email: /crm/v4/objects/{fromObjectType}/{fromObjectId}/associations/default/{toObjectType}/{toObjectId}
So, you’re call might look something like this:

https://api.hubapi.com/crm/v4/objects/tickets/12345/associations/default/emails/678910

where the fromObjectID (ticket id) is 12345 and the toObjectID (email id) is 678910.

I hope this helps. More robust coding will likely need @Anton’s expertise.
Thanks for the tag, @PamCotton.

All the Best,

Ryan Schweighart

Whole Hart Impact, LLC

whimpact.co

I help organizations use HubSpot and Zapier.

But the problem i am facing how to create email, if using this API reference and test Accounts Dashboard | HubSpot , it asked for association. if using code still having problem creating email.

Can guide where to get proper resource

I need to create email and associate with ticket whenever new ticket created in the system.

I tried creating email via api end point

https://api.hubapi.com/marketing-emails/v1/emails

It created email successfully with emailId returned. when I sent request to API endpoint you shared

/crm/v4/objects/tickets/{ticketId}/associations/default/emails/{emailId}

It throw error response

One or more associations are invalid, “INVALID_OBJECT_IDS”:[“EMAIL=187471576381 is not valid”],“objectId”:[“187471576381”]

I somehow succeeded creating email using hubspot API (https://api.hubapi.com/crm/v4/objects/emails) and associate it with ticket.

Now trying to associate email with From / To Contacts using API but it failed (Not Found) throw

https://api.hubapi.com/crm/v4/objects/emails/{emailId}/associations/contacts/{fromContactId}/{associationType}

WHERE emailID => Newly created email

contactID => ID of contact retrieved via hubspot contact api

associationType => I tried ‘FROM’ / ‘TO’ or ‘EMAIL_FROM’ , / ‘EMAIL_TO’

Hi @MWebb5,

If you’re associating records without a label using v4, I don’t think you need the ‘associationType’ at the end of the URL. Also, try ‘contact’ instead of contacts.

It should be very similar to however you associated the email with the ticket.

All the Best,

Ryan Schweighart

Thank you for hints, using same code as used for associating email with tickets. it worked.

Email to Contact

Object

var association = new
{
emailID,
objectId = contactID,
objectType = “CONTACT”
};

End Point

/crm/v4/objects/contact/{contactID}/associations/default/emails/{emailID}

Email to Ticket

Object

var association = new
{
emailID,
objectId = ticketId,
objectType = “TICKET”
};

/crm/v4/objects/tickets/{ticketId}/associations/default/emails/{emailID}