I have a doctype that contains two identical child tables with different fieldnames. I want both child table to act identically, including my custom functionality. Instead, my on_change events are only triggering on the first field. This is how I’ve set up the code:
frappe.ui.form.on('Childtable Doctype', {
my_field: (frm, cdt, cdn) => {
console.log('logs only when the first child table my_field is triggered');
},
If I change the value in with my_field for the first child table, I see the message logged to the console. However, if I interact with the second child table, nothing happens.
Based on how the database respects the fieldname of the Child Doc, I expected the form events to work the same way - the functionality would either be attached to both forms, or there would be a way to explicitly to attach it to both forms.
How can I get my on_change events to work on the second child table?
same problem any solution?
If you have added same child table doctype 2 times in a same doctype then it will create issues. For that you have to understand DB table structure. though you have added child tables 2 times in a doctype, there is only one DB table for that child table. When the doctype with values in child table is saved, both child table’s data is saved in single database table with reference to parent doctype.
Better solution: Duplicate the child doctype, give some different name to it. Add these child doctypes to your parent doctype. And you need to code to reflect changes made in one child table into other child table
It shouldn’t create issues. The data stored in the database references not only the doctype but also the field name that generated the data. My issue wasn’t with with the data being saved or retrieved - that worked fine - only with Javascript running on the second child field.
Looking at the definition of frappe.ui.form.on
and using what I’ve learned in the intervening years, it looks like it can take a third parameter. When that happens, the second parameter is the fieldname - it looks like an undocumented feature. Something like this:
frappe.ui.form.on('Childtable Doctype', 'my_fieldname', {...})
Unfortunately I’m long past this issue and I don’t have time to test my discovery. Still, maybe someone here can try this and comment for everyone’s benefit?