How to pass parameters to doctype Action

I am trying to create a custom doctype with a action button. What I did was I created an action type of server action and action is to call an whitelisted function, e.g. custom_module.custom_function.myaction. It runs fine if my whitelisted function doesn’t accept parameter.

However, I need the whitelisted function to be able to accept parameters when we press the action button. So I changed the my function to:

@frappe.whitelist()
def myaction(message):

But I have no idea how I can pass the parameter from the action. When I tried custom_module.custom_function.myaction(message), and I am trying to pass the field ‘message’ in my doctype, it just have a message of:

Failed to get method for command custom_module.custom_function.myaction(message) with module ‘custom_module.custom_function’ has no attribute ‘myaction(message)’

Any idea? Thanks!

2 Likes

You can do a action that opens a popup, then pass the parameter from the popup to the function?

So passing parameter directly from the action is not possible?

I think the button just trigger a function to run. So it depends on where you get the parameter from:

  • if from the document itself, you can use frm.doc.fieldname in js to get the data and pass it to py with frappe.call
  • if it is from the py file, just pass it as parameter to the function.
  • if you want to entry the parameter, use the popup as suggested.
  • etc…
1 Like

I think you want to do this. Here is an example that passes 3 fields of a form view as parameters (args) to a whitelisted function in a client script:

frappe.ui.form.on('My DocType', {
	my_button(frm) { // Event for pressing button "My Button"
        frappe.call({
            method: "erpituc.examples.doctype.call_server_method.api.get_simple_data",
            args: {
              "project" : frm.doc.project,
              "project_name" : frm.doc.project_name,
              "start_date" : frm.doc.start_date
            },
            callback: function(r) {
                msgprint(__("Simpler Returnwert: {0}", [r.message]));
            }
        });
    }
};

In the py-File you access the parameters like this:

@frappe.whitelist()
def get_simple_data(**args):
    project = args.get('project')
    project_name = args.get('project_name')
    start_date = args.get('start_date')

    return project + ", " + project_name + ", " + start_date
1 Like

Yes. I use this way and it works. Thanks.

I was exploring the doctype Action way since it look easier. However, if we can’t pass the parameters, then it is not suitable for my use.

1 Like