How to delete/refresh/clear child table entries/data from parent doctype while updating

Hello people
I have a childtable that get auto populated based on the fields of the parent doctype

What I want is the entries/rows of the childtable should get added and deleted on the basis of the updated values of parent table

So what I am looking for is how can I refresh my child table list
Thank you

@Harsh_Magiya You can clear the child table like this…

frm.set_value('child_table_fieldname', []);
frm.refresh_field('child_table_fieldname');

For example, if the name of the parent field is parent_field, then your code should look something like this…

frappe.ui.form.on('Doctype', {
    parent_field: function(frm) {
        if (!frm.doc.parent_field) {
            frm.set_value('child_table_fieldname', []);
            frm.refresh_field('child_table_fieldname');
        }
    }
});
1 Like

Hey your code has a term child_table_fieldname
So just to confirm this will clear all the entries/rows in a child table, right?

@Harsh_Magiya Yes.

  • Replace Doctype with the name of your doctype…
  • Replace parent_field with the fieldname of the field that triggers the code when changed
  • Replace child_table_fieldname with the fieldname of the child table in the parent doctype
1 Like

Thank you for the explanation

@Harsh_Magiya You are welcome buddy…

If my code above is working and fixed your problem please mark it as the Answer so it becomes easier for others to find it…

Hey
Sorry I was bit occupied so could not the code
Actually I want to add this functionality in a button
so i tried to modified the code but seems that does not work

Code in my custom_doctype(demo2).js file
frappe.ui.form.on('demo2', { refresh: function(frm) { frm.add_custom_button(__('Delete'), function() { frm.set_value('table', []); frm.refresh_field('table'); } } });

The fields of my parent_doctype(demo2)

@Harsh_Magiya Try the same code but without frm.refresh_field('table'); because there is no need for it…

Or you can use the same code but replace frm.set_value('table', []); with frappe.model.clear_table(frm.doc, 'table');

Hey I tried to do both of them separately and together too but it didnot work

@Harsh_Magiya That’s weird…

Try this…

frappe.ui.form.on('demo2', {
    refresh: function(frm) {
        frm.add_custom_button(__('Delete'), function() {
            console.log('Table - Total number of rows before delete', frm.doc.table.length);
            //frm.set_value('table', []);
            frappe.model.clear_table(frm.doc, 'table');
            frm.refresh_field('table');
            console.log('Table - Total number of rows after delete', frm.doc.table.length);
        });
    }
});

If the code works then:

  1. The delete button will be created
  2. When clicking the delete button, two messages will be posted into the console of the browser, they will state the table’s total number of rows before and after the process

The code should work unless the client script is not for the form of demo2 doctype or the child table fieldname isn’t correct.

1 Like

Hey I copied this code and pasted in my demo2(custom_doctype).js file
and seems like the script is incorrect cause my screen goes blank

So just to confirm writing code in client script from frontend and writing js script from backend is same right?
Also I hav uploaded my doctype screenshot for reference/confirmation above

@Harsh_Magiya The script shouldn’t have caused the form to be empty…

How about you try the following code…

frappe.ui.form.on('demo2', {
    onload: function(frm) {
        frm.add_custom_button(__('Delete'), function() {
            console.log('Table - Total number of rows before delete', frm.doc.table.length);
            frm.set_value('table', []);
            if (frm.doc.table.length) {
                frappe.model.clear_table(frm.doc, 'table');
            }
            console.log('Table - Total number of rows after delete', frm.doc.table.length);
        });
    }
});

Hey @kid1194
sorry while pasting the code I might have pressed a button and that made the syntax incorrect
The above code is correct
Just 2 more things

  1. No msg came like you said
    When clicking the delete button, two messages will be posted into the console of the browser, they will state the table’s total number of rows before and after the process
  2. What if after deleting I want to run a function from .py file
    how can I add that

@Harsh_Magiya Use the following code which works…

frappe.ui.form.on('demo2', {
    onload: function(frm) {
        frm.add_custom_button(__('Delete'), function() {
            console.log('Table - Total number of rows before delete', frm.doc.table.length);
            frm.set_value('table', []);
            console.log('Table - Total number of rows after delete', frm.doc.table.length);
            // Here you can add anything you want to call after the table is cleared
        });
    }
});

The logs should be shown in the browser’s console…

Hey idk why but onload doesnt work and refresh works in the second line

@Harsh_Magiya I’ve no idea why it’s not working…

But since refresh is working for you then use the following code…

frappe.ui.form.on('demo2', {
    refresh: function(frm) {
        if (!frm._del) {
            frm._del = 1;
            frm.add_custom_button(__('Delete'), function() {
                console.log('Table - Total number of rows before delete', frm.doc.table.length);
                frm.set_value('table', []);
                frappe.model.clear_table(frm.doc, 'table');
                console.log('Table - Total number of rows after delete', frm.doc.table.length);
                // Here you can add anything you want to call after the table is cleared
            });
        }
    }
});

Thank you