how can I add comment to a doc from js? (I need to add comment to a doc on its submission, from js). I tried cur_frm.add_comment, cur_frm.cscript.add_comment, doc.add_comment, this.add_comment and none of them were recognized.
1 Like
cur_frm.comments.insert_comment("Comment","My Comment")
2 Likes
You can do this by writing a whitelisted function . and write code as in below in server side
d = frappe.get_doc(‘Opportunity’, name)
#to add a new comment
d.add_comment(‘Comment’,text=‘Test Comment’)
it will add a new comment ‘Test Comment’ in Opportunity and doc.name=name
2 Likes
I think v14 doesn’t support that.
I wanted to add a comment from a dialog. Here’s my hack:
function add_comment(frm, title='Add a Comment'){
// return new Promise((resolve, reject) => {
const dialog = new frappe.ui.Dialog({
title: __(title),
fields: [],
});
// Create a container for the comment box
const commentBoxWrapper = $('<div class="comment-box" style="margin-top:25px;"></div>').appendTo(dialog.body);
// Initialize the comment box
const commentBox = frappe.ui.form.make_control({
parent: commentBoxWrapper[0], // Ensure the DOM element is passed
render_input: true,
only_input: true,
enable_mentions: true,
df: {
fieldtype: "Comment",
fieldname: "comment",
},
on_submit: (comment) => {
if (strip_html(comment).trim() !== "" || comment.includes("img")) {
commentBox.disable();
frappe
.xcall("frappe.desk.form.utils.add_comment", {
reference_doctype: frm.doc.doctype,
reference_name: frm.doc.name,
content: comment,
comment_email: frappe.session.user,
comment_by: frappe.session.user_fullname,
})
.then((new_comment) => {
cur_frm.reload_doc();
// Close the dialog
dialog.hide();
// resolve(true); // Comment successfully added
})
.catch((error) => {
frappe.msgprint(__('Failed to add comment.'));
// reject(new Error(error));
})
.finally(() => {
commentBox.enable();
});
} else {
frappe.msgprint(__('Please enter a valid comment.'));
// reject(new Error('Invalid comment'));
}
},
});
// Render and display the comment box
commentBox.refresh();
// Customizations after rendering
setTimeout(() => {
const modal = dialog.$wrapper.find('.modal-content');
if (modal.length && window.innerWidth >= 768) {
modal.css({
width: '800px',
left: '50%',
transform: 'translateX(-50%)'
});
}
const box = dialog.$wrapper.find('.comment-box');
if (box.length && window.innerWidth >= 768) {
box.css({
'margin': '20px',
'margin-top': '25px'
});
}
// Remove the .comment-input-header
const header = commentBoxWrapper.find('.comment-input-header');
if (header.length) {
header.remove();
}
// Adjust the input box height to display 3 lines
const editor = commentBoxWrapper.find('.ql-editor');
if (editor.length) {
editor.css({
height: '5em', // Adjust height for ~3 lines
overflowY: 'auto',
});
}
}, 100);
// Show the dialog
dialog.show();
});
}
It’s a pretty nice dialog
- it adapts on desktop/mobile
- it allows tagging users using ‘@’
- it reuses the comment field elements and hacks it a bit
Simply call it from anywhere like so
add_comment(frm, 'title string');
2 Likes