Associating an incoming email to a deal using custom code (Operations Hub)

We recently upgraded to operations hub pro and service hub pro. I was told this would give me the ability to have more control over how incoming emails are associated to deals, particularly via custom code.

Basically my problem is the following. All emails we send out contain a specific reference (e.g. C434-XRB2) in the subject line. This same reference is present in the deal name. Whenever an email comes in, I want to scan its subject line for a reference (using a regex or something) and then search if there are any deals present with this reference. If so, I want to associate the email with the matching deals.

I have made a first attempt using custom code workflows in operations hub, but I get stuck by my lack of knowledge of Hubspot internal structure. Particularly, what entity should trigger the workflow? The creation of a conversation, or at ticket? There doesn’t appear to be a specific trigger for emails.

If the conversation is the trigger, how do I retrieve the specific email to scan its subject? The event contains an objectId, but the conversations API is still in Beta, and doesn’t appear to offer a way to retrieve an email simply by an objectId; you need a thread id as well.

In short, is there anyone that can help me on my way with this, or give some pointers on what is the best way to start? Many thanks!

@cargoplot - I use this kind of dynamic custom filtering of conversations a lot! The basis of the approach is to use a Conversations workflow and parse the internals of the Conversation object to get hold of the message details. Consider using a trigger criteria for the workflow which is tied to the Inbox ID and uses Source equal to Email. For the Custom code step, consider using ThreadID as a parameter and use the conversations/threads API.

The structure is far from well documented, but essentially for inbound emails like this the data resolves into a series of MESSAGES where each message has structure for ‘from’, ‘to’, ‘subject’ etc.

Good luck on this.

Steve

Hi @SteveHTM

Thanks for your reply. Those are good tips. I am only wondering how I can get a thread ID from the conversations event data. I didn’t see it when I inspected the fields of this object. Or did I miss it? And if I assign the thread to the deal, then all consequent emails in this thread will also be assigned to this deal?

Thread ID is a property on the conversations object. As mentioned above, I pass that value to the custom code workflow as a parameter.