Add custom button for any doctype

Hello, everyone. Is it possible to add a custom button in Frappe for a form whose type is chosen by the user?

For context: I am developing an application to generate documents based on Word and Excel templates. Visually, the setup looks like this:


image

but I will create this group of custom buttons by adding a script to the pms People dock. I’m looking for a way to add buttons for any docktype, based on the previously specified information in the settings from the first screenshot. Can anyone help me with this?

Hi @maks4a ,

Yes we can.

Can you tell us how?

Hi @maks4a ,

frappe.ui.form.on('pms People', {
    refresh: function(frm) {
        frappe.call({
            method: 'frappe.client.get_list',
            args: {
                doctype: 'Your_Settings_Doctype',
                filters: { doctype_name: frm.doctype },
                fields: ['title', 'template']
            },
            callback: function(response) {
                if (response.message) {
                    response.message.forEach(row => {
                        frm.add_custom_button(row.title, function() {
                            generate_document(frm, row.template);
                        }, __('Actions'));
                    });
                }
            }
        });
    }
});

function generate_document(frm, template_path) {
    let url = `/api/method/your_app.your_module.generate_document?docname=${frm.doc.name}&template=${template_path}`;
    window.open(url);
}

import frappe

@frappe.whitelist()
def generate_document(docname, template):
    doc = frappe.get_doc("pms People", docname)
    
    file_url = process_template(doc, template)
    
    return file_url

def process_template(doc, template_path):
    return "/private/files/generated_document.docx"

1 Like

(post deleted by author)