I originally asked this in the Telegram, and @Ritvik_Sardana has been helping me, but I thought moving this here would help others who may have a similar issue or want to implement this in the future.
We have multiple email addresses that all feed into the Helpdesk, creating tickets. We want to update the “Ticket Type” and “Team” fields when a ticket is created to match the email account it came from.
Ritvik provided this script for the first email account, which does seem to work:
communication_exits = frappe.db.exists("Communication",{"reference_name":doc.name})
if communication_exits:
email_account = frappe.db.get_value("Communication",communication_exits,"email_account")
if (email_account == “name-here”):
doc.agent_group = “Billing Office”
doc.ticket_type = “Business Team”
Where is this code getting called? At first glace, it looks like you might be editing the properties of doc but not actually saving or committing the changes.
You’re possibly running into a race condition of some sort, though it’s hard to say exactly what. It’s tough to help troubleshoot with the information here. I don’t know what it means that the script is having trouble finding the right email.
It definitely reads like a race condition, I am not sure how to solve it though. I know I am not the only one with this need, (How to manage multiple Email with different assignment agent - #3 by Prasath_Sekar is asking for similar). I am open to suggestions on how to troubleshoot and fix this. This is the final hold-up for us to go live.
Does “Before Insert” sound like the right trigger? Like I said, I tried most of them, but since this is most likely a race condition I am looking for community thoughts here.
I’m still not really sure what’s happening when you say that the script is “having trouble finding the correct Communication”. Exactly what’s happening (or not happening) is the key to troubleshooting this.
I just checked how I do it on my system. We feed a few different Email Addresses into HD Tickets (via the Append To field).
We then use a server script on the Communication doctype (not the HD Ticket doctype) using the After Insert hook:
if doc.reference_doctype == "HD Ticket" and doc.reference_name:
if not frappe.db.get_value("HD Ticket", doc.reference_name, "email_account"):
frappe.db.set_value("HD Ticket", doc.reference_name, "email_account", doc.email_account)
if not frappe.db.get_value("HD Ticket", doc.reference_name, "agent_group"):
if doc.email_account == "Email Account 1":
frappe.db.set_value("HD Ticket", doc.reference_name, "agent_group", "Agent Group 1")
elif doc.email_account == "Email Account 2":
frappe.db.set_value("HD Ticket", doc.reference_name, "agent_group", "Agent Group 2")
elif doc.email_account == "Email Account 3":
frappe.db.set_value("HD Ticket", doc.reference_name, "agent_group", "Agent Group 3")
It works consistently.
I don’t know how you’re currently generating tickets, but if the HD Ticket is being created as part of the Communication creation process, it’s very possible that the Communication doc hasn’t yet been finalized when HD Ticket gets inserted. That might explain what you’re seeing.