adam26d
December 13, 2022, 11:29am
1
I am trying to add an email to the timeline section, similar to how comments are added. Not sure how to go about it.
To be clear, this email is sent using an external app, that record is being synched into ERPNext and I want to show it under the Lead Doctype.
I want to do something like this
You can easily add a comment with doc.add_comment('Comment', text='Test Comment')
Any ideas how to add an email?
adam26d
December 13, 2022, 2:35pm
2
I think this should do it
if not updated:
_comments.append(
{
"comment": get_truncated(doc.content),
# "comment_email" for Comment and "sender" for Communication
"by": getattr(doc, "comment_email", None) or getattr(doc, "sender", None) or doc.owner,
"name": doc.name,
}
)
update_comments_in_parent(doc.reference_doctype, doc.reference_name, _comments)
if doc.reference_doctype and doc.reference_name and doc.content:
_comments = get_comments_from_parent(doc)
updated = False
for c in _comments:
if c.get("name") == doc.name:
c["comment"] = get_truncated(doc.content)
updated = True
if not updated:
_comments.append(
{
"comment": get_truncated(doc.content),
# "comment_email" for Comment and "sender" for Communication
"by": getattr(doc, "comment_email", None) or getattr(doc, "sender", None) or doc.owner,
"name": doc.name,
}
)
update_comments_in_parent(doc.reference_doctype, doc.reference_name, _comments)
adam26d
December 14, 2022, 11:52am
3
Turns out you only need to insert a Communication doctype entry
comm = frappe.get_doc({
'doctype': 'Communication',
'subject': campaign.title,
'communication_medium': 'Email',
'sender': campaign.sender,
'recipients': doc.email,
'content': 'Campaign Subject: ' + campaign.title,
'text_content': None,
'communication_type': 'Communication',
'status': 'Linked',
'sent_or_received': 'Sent',
'communication_date': now(),
'sender_full_name': 'Mailjet',
'reference_doctype': 'Lead',
'reference_name': lead_name,
'reference_owner': 'Administrator',
'user': 'Administrator',
'message_id': '',
'email_status': 'Open',
'has_attachment': 1,
'docstatus': 1,
})
comm.insert(ignore_permissions=True)