Frappe List View Actions Button

I have a doctype called “Doc A”. I want to add a list view action button which does a couple of things. Here is my code so far:

  frappe.listview_settings['Doc A'] = {

   onload(listview) {
        // triggers once before the list is loaded
        console.log("loaded", listview);
        listview.page.add_action_item('Do this', () => my_action());
       
  }
}

Now what I want my_action to do is:

  1. Take all selected documents and change a value in them. Let’s say I have a field called fielda. I want this button to set the value of fielda to “ABC”
  2. Take all selected Documents and map them onto a child table link field (The link field pertaining to the selected documents. I’m guessing this works in some reverse form of a frappe call)
  3. Create a document each for all selected Doc A and create a Doc B (another doctype with a link field to Doc A).

Any help is appreciated. Thanks. :grinning:

Note: I have been trying to reverse engineer already present list view actions like “Edit”, “Delete” , “Apply Assignment Rule” but I can’t seem to find the code for it. If anyone knows where that is, that’d be helpful too.

1 Like

@Vesper_Solutions,

Did you find out how to do this? I need my own list view action to operate on all selected documents in the list view and would be interested to hear how you did step 1.

May be this PR can help you.

2 Likes

Thank you very much, @mujeerhashmi!

I now have this code in a list view custom script, which demonstrates the principle:

frappe.listview_settings['Sales Invoice'].onload = function(listview) {
    // add button to menu
    listview.page.add_action_item(__("Test option"), function() {
    	test( listview );
});
};

function test( listview )
{
	let names=[];
	$.each(listview.get_checked_items(), function(key, value) {
		names.push(value.name);
	});
	if (names.length === 0) {
		frappe.throw(__("No rows selected."));
	}
			
	frappe.msgprint( names );
}

I could not find get_checked_items() anywhere in the Frappe documentation. Perhaps I just need to explore the Frappe code instead.

4 Likes

Thanks everyone for the links and code. Just to add info for anyone who needs list buttons:

  • you can use the list js file: List

  • you can use get_checked_items if you need to work with selected documents

  • inside onload you can add buttons: using the Page API

  • use Frappe Server calls (AJAX) or something to get current user permissions and make button visible or not.

  • Don’t forget to always review security permissions in the backend, especially if it’s an external api.

1 Like

Thank you, this really helped me.