Server Script to Autoassign HD Ticket created via email

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”

I updated this code to add the additional emails:

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 == "Valley Ambulance Authority Business Office"):
        doc.agent_group = "Billing Office"
        doc.ticket_type = "Business Team"
    elif (email_account == "Valley Ambulance Authority Chiefs"):
        doc.agent_group = "Chiefs"
        doc.ticket_type = "Operations"
    elif (email_account == "Valley Ambulance Authority IT"):
        doc.agent_group = "Technology"
        doc.ticket_type = "Technology"
    else:
        doc.ticket_type = "Unspecified"

This script does not work as expected, with all tickets getting assigned to the Billing Team still

This same code, works when used in the “System Console”

communication_exits = frappe.db.exists("Communication",{"reference_name":"51"})
if communication_exits:
    email_account = frappe.db.get_value("Communication",communication_exits,"email_account")
    
    if (email_account == "Valley Ambulance Authority Business Office"):
        print("billing")
        
    elif (email_account == "Valley Ambulance Authority Chiefs"):
        print("chiefs")
        
    elif (email_account == "Valley Ambulance Authority IT"):
        print("tech")
    else:
        print("other")

Hi there,

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.

It’s in a server script, and the doc is definitely getting saved, because tickets are getting assigned, just always to the “Billing Team”.

What kind of server script? If you disable the script, do new tickets go unassigned?

“Before Insert”

Yes, they are unassigned with the script disabled.

And like I said with the script, they all go to one team no matter how many if statements I add

I did some additional digging, and found that the script is having trouble finding the correct Communication and so its not finding the right email.

I added some additional filtering, and log statements, and even tried different events (Started with Before Insert), but no luck on any of them:

if not doc.custom_parsed:
    frappe.log_error(f"I am debugging this {doc.name} - Doc Name")
    communication_exists = frappe.db.exists("Communication",{"reference_name": doc.name, "reference_doctype": "HD Ticket"})
    frappe.log_error(f"I am debugging this {communication_exists} - Communication Exists")
    if communication_exists:
        frappe.log_error(f"I am debugging this {communication_exists} - {email_account} - Email Account")
        email_account = frappe.db.get_value("Communication",communication_exists,"email_account")
        
        if (email_account == "Valley Ambulance Authority Chiefs"):
            doc.agent_group = "Chiefs"
            doc.ticket_type = "Operations"
        elif (email_account == "Valley Ambulance Authority Business Office"):
            doc.agent_group = "Billing Office"
            doc.ticket_type = "Business Team"
        elif (email_account == "Valley Ambulance Authority IT"):
            doc.agent_group = "Technology"
            doc.ticket_type = "Technology"
        else:
            doc.ticket_type = "Unspecified"
        doc.description = email_account
        doc.custom_parsed = True

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.