Field type button

I would like to understand how do I use the option to add a field type “button” to a form, and how do I determine it’s purpose.
thanks.

Hi @Ayelet ,

To add a button Go to Setup → Customize form → choose the form where you want to add button → click the add new row button → field type by default will be data , choose button there .

To determine it"s purpose refer the below example script

 frappe.ui.form.on("Event", "refresh", function(frm) {
    frm.add_custom_button(__("Do Something"), function() {
        // When this button is clicked, do this

        var subject = frm.doc.subject;
        var event_type = frm.doc.event_type;

        // do something with these values, like an ajax request 
        // or call a server side frappe function using frappe.call
        $.ajax({
            url: "http://example.com/just-do-it",
            data: {
                "subject": subject,
                "event_type": event_type
            }

            // read more about $.ajax syntax at http://api.jquery.com/jquery.ajax/

        });
    });
});
1 Like

I have zero experience with scripting. so I would need a more elaborated answer :slight_smile:
where is this script written?
and how do I determine the button’s purpose?

I had very little experience with coding when I started with ERPNext. I suggest https://www.codecademy.com/ to get started. If you want to do anything truly custom in ERPNext you will need to work with and learn some code.

Going through the manual and Frappe Framework tutorial is a good place to start. Although if you’re not hosting ERPNext yourself then learning Frappe is not necessary.

If you’re on a hosted account all scripts are in the “custom scripts” doctype in the “setup” module. Scripts are made for each form they will be used on. So if you made a custom button field in the “Purchase Order” doctype, you need to make a custom script for the “Purchase Order” doctype. There is only 1 script allowed per doctype so all your functions must be contained in that 1 script.
Scripts are written in Javascript with a splash of SQL when people are in the mood. Until you start to understand Javascript you will be fully reliant on the good graces of others for help.

To run a script on a custom field button when it’s pressed you can use the following script to start:

frappe.ui.form.on("[DocType]", "[button_fieldname]", function(frm) { 
});

[Doctype] should be replaced with the name of the doctype the custom button field is on.
[button_fieldname] should be replaced with the name of the button.
What you want the button the do should be coded in Javascript, and goes between the curly braces { }

Have fun learning to code!

9 Likes

This was useful!

Thank you!
How can I do the same with Server Side => Python?

Hello… question from a newbie. Where should i put the script for the button?