Action for fieldtype Button

Elaborate please.

#1 using the server script, I don’t know how to change the display of checked checkbox after setting the value to 1.

#3 using the js to catch the button click and send it to py code via frappe call. But since the doctype_name.py file is in the doctype directory (in app/app/doctype/doctype_name.py) I don’t know how to write the path because frappe doesn’t recognize a py file in doctype directory of a custom app.

Use something like

doc = frappe.get_doc(doctype, docname)
doc.check_box = 1
doc.save()
doc.reload()

Frappe does recognize it. You path will be

app.module.doctype.doctype_name.function_name
2 Likes

OK. Will try this.

Yes I use this structure but the error always says there is no function_name in that path.

But let me try again…

Did you whitelist the function?
https://frappeframework.com/docs/user/en/guides/basics/frappe_ajax_call#calling-whitelisted-functions

Add @frappe.whitelist() just above your function.

If I do the code in the doctype_name.py, I don’t have to do this right?
And it is for a new doc.

Tried both whitelisted and not. Still the same.

If you’re in the document class, you can use self. instead of doc.

Can you show your code and a SS of the error?

OK… tried it just now and it is the self.reload() that I need.

I will exercise with the js and frappe.call to see if I made a mistake in the method path.

Thanks @rtdany10

1 Like

Hi @rtdany10,
How to call the method within a class?
If the def is outside the class it can be called (whitelisted or not) using the path, but not if inside a class.

Example:

class Member(Document):
    def button_a(self):
        checkbox_a = 1

def button_b(self):
    checkbox_b = 1

The path app.module.doctype.member.member.button_a returns

AttributeError: module 'app.module.doctype.member.member' has no attribute 'button_a'

While the path app.module.doctype.member.member.button_b is able to run the method.

Let abc be a document of type Member

doc = frappe.get_doc('Member', 'abc')
doc.button_a()

And how to put it as path for use in frappe.call() method ?

You can only call whitelisted functions with frappe.call(), not class.
What is your use case tho?

I have a fieldtype button which I process the method directly in py file of the doctype. Not in js.
But since I want to change the hidden/show of other fields, I need to do it in js, right?
Is there a way to change field property directly from py?

As my initial post, there are 3 ways to link method to button. Each has their own limitation (as I know, and I’m not a developer :pray: )

So the last post of mine was in related to calling a py method from frappe.call(), where that method is inside a class. So it can return a value to js to change the field property.

My point is, why are you keeping it as a method? Why not a function? I don’t see any advantage.

frappe.call() can be used to call only whitelisted functions.

Hey @rahy
Can you elaborate a bit on how can I connect a button field type to its method in .py file
Thank you

You can use the frappe.call() to send values to the related python file.
In frappe.call() specify the method parameter with the dotted path to the function in the py file.

Okay are the talking about the doctype fieldtype button availabe in v-14?

if yes can you show a small example of how can I write the code in the custom_doctype.py file
under
class custom_doctype(Document):
def butto(self):
frappe.msgprint(“Success”)

This thread might help

1 Like