Using this → Attachments arent going with emails
frappe.ui.form.on(“Sales Invoice”, {
refresh(frm) {
if (frm.doc.docstatus === 1 && !frm.is_new()) {
frm.add_custom_button(“Request Cancellation”, () => {
const user_name = frappe.session.user_fullname || frappe.session.user;
const user_email = frappe.session.user_email || frappe.session.user;
const invoice_link = `${window.location.origin}/app/sales-invoice/${frm.doc.name}`;
const footer_html = `
<br><br><br><br>
<p><a href="${invoice_link}" target="_blank">View Invoice</a></p>
<br>
<p>Regards,<br>${user_name}<br>${user_email}</p>
`;
// Collect current attachments from the document
const attachments = (frm.attachments?.get_attachments() || []).map(f => ({
name: f.name,
file_name: f.file_name
}));
const composer = new frappe.views.CommunicationComposer({
doc: frm.doc,
subject: `Request Cancellation: ${frm.doc.name}`,
recipients: frm.doc.owner,
message: footer_html,
title: "Add Cancellation Request",
send_email: 1,
attachments: attachments, // 👈 include existing doc attachments
send_action: function() {
// Validate "Send me a copy"
if (!composer.dialog.fields_dict.send_me_a_copy.get_value()) {
frappe.throw("You must enable 'Send me a copy'.");
}
// Validate message content
const message_content = composer.dialog.fields_dict.content.get_value()
.replace(/<br\s*\/?>|\s+/g, "")
.replace(/<[^>]*>/g, "");
if (!message_content) {
frappe.throw("Message cannot be empty.");
}
// Send the email with attachments
frappe.call({
method: "frappe.core.doctype.communication.email.make",
args: {
doctype: frm.doc.doctype,
name: frm.doc.name,
recipients: composer.dialog.fields_dict.recipients.get_value(),
subject: composer.dialog.fields_dict.subject.get_value(),
content: composer.dialog.fields_dict.content.get_value(),
send_email: 1,
attachments: attachments // 👈 send with attachments
},
callback: function(r) {
if (!r.exc) {
frappe.msgprint("Cancellation request email sent");
}
}
});
composer.dialog.hide();
},
});
// Auto-check and require "Send me a copy"
composer.dialog.fields_dict.send_me_a_copy.set_value(1);
composer.dialog.fields_dict.send_me_a_copy.df.reqd = 1;
});
}
}
});