Binding button event with DocType class method

I has surprised to see the following,

and also in purchase_receipt.py ,
Uploading…
The uploading image is stack… apps/erpnext/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py, code line 169 , ‘get_current_stock’

So, can I assume that button ‘action’ can always be linked to a backend method of the DocType if I provide the method name as option?

Is this somewhere documented ?

Testing it with “Button Tester” Doctype

 "fields": [
  {
   "fieldname": "button_test", 
   "fieldtype": "Button", 
   "label": "Button Test", 
   "options": "button_test", 
   "print_hide": 1
  }
 ]

button_tester.py

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class ButtonTester(Document):

	def button_test(self):
		frappe.msgprint("Button Test")

It is not working.

Ty @revant_one for your Test Doc. I will try change the method to something like ‘get_button_test’. I think your test didn’t work due namespace , ‘fieldname’ == ‘options’ .

I will try.

@revant_one I was right , if you make something like ‘get_button_test’ it will work. I think in your example you are calling the ‘on change’ method of fieldname.

"fields": [
  {
   "fieldname": "button_test", 
   "fieldtype": "Button", 
   "label": "Button Test", 
   "options": "get_button_test", 
   "print_hide": 1
  }
 ]
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document

class TestButton(Document):

	def get_button_test(self):
		frappe.msgprint("Button Test")
3 Likes

It works!

Thanks

This can be added under App Development Section of frappe documentation. @kickapoo you wish to do that?
https://github.com/frappe/frappe/tree/develop/frappe/docs/user/en/guides/app-development

@revant_one I will do, i think I will title it: 'Binding button event with DocType class method"

FYI : I wanted to test the data binding in client side to make sure …
So, if you add a Data Field with name ‘button_result’

   def get_button_test(self):
       self.button_result = "You clicked a button"
4 Likes