diff --git a/frappe/core/doctype/communication/communication.py b/frappe/core/doctype/communication/communication.py index 2c5cf4889a..88d1e5c32f 100644 --- a/frappe/core/doctype/communication/communication.py +++ b/frappe/core/doctype/communication/communication.py @@ -416,6 +416,13 @@ class Communication(Document, CommunicationEmailMixin): # Timeline Links def set_timeline_links(self): + # Skip timeline links if a "Sent" communication already exists + # else will create duplicate timeline entries + if self.sent_or_received == "Received" and self.find_one_by_filters( + message_id=self.message_id, sent_or_received="Sent" + ): + return + contacts = [] create_contact_enabled = self.email_account and frappe.db.get_value( "Email Account", self.email_account, "create_contact" diff --git a/frappe/email/doctype/email_account/test_email_account.py b/frappe/email/doctype/email_account/test_email_account.py index af5749b309..96a02c86dd 100644 --- a/frappe/email/doctype/email_account/test_email_account.py +++ b/frappe/email/doctype/email_account/test_email_account.py @@ -523,7 +523,7 @@ class TestInboundMail(IntegrationTestCase): mail_content = self.get_test_mail(fname="incoming-1.raw") message_id = Email(mail_content).message_id # Create new communication record in DB - communication = self.new_communication(message_id=message_id) + communication = self.new_communication(message_id=message_id, sent_or_received="Received") email_account = frappe.get_doc("Email Account", "_Test Email Account 1") inbound_mail = InboundMail(mail_content, email_account, 12345, 1) diff --git a/frappe/email/receive.py b/frappe/email/receive.py index 6733c2d246..aeca829199 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -720,7 +720,9 @@ class InboundMail(Email): if not self.message_id: return - return Communication.find_one_by_filters(message_id=self.message_id, order_by="creation DESC") + return Communication.find_one_by_filters( + message_id=self.message_id, sent_or_received="Received", order_by="creation DESC" + ) def is_sender_same_as_receiver(self): return self.from_email == self.email_account.email_id @@ -766,7 +768,9 @@ class InboundMail(Email): if not self.is_reply(): return "" - communication = Communication.find_one_by_filters(message_id=self.in_reply_to) + communication = Communication.find_one_by_filters( + message_id=self.in_reply_to, order_by="creation DESC" + ) if not communication: if self.parent_email_queue() and self.parent_email_queue().communication: communication = Communication.find(self.parent_email_queue().communication, ignore_error=True)