Is there a way to use the triggers: “refresh” or “onload” on a child table? I know how field based triggers work and stuff but I cant get my script to work for a child table based on a refresh or an onload trigger.
i’ve already tried using ***_on_form_rendered: but that doesnt work.
How is your code look like?
My child table name is item. The parent doctype is invoice.
I’ve tried using both cases:
frappe.ui.form.on("Invoice", {
item_on_form_rendered: function(frm, cdt, cdn) {
// code
}
});
and
frappe.ui.form.on("item", {
on_form_rendered: function(frm, cdt, cdn) {
// code
}
});
Neither works
What do you want to achieve?
Any simple code would do, let’s say there are three variables and I want to calculate a difference for each. Example:
frappe.ui.form.on("item", {
on_form_rendered: function(frm, cdt, cdn) {
var z = locals[cdt][cdn];
frm.doc.plan_budget.forEach(function(z) {
frappe.model.set_value(z.doctype, z.name, "remaining", z.total - z.delivered);
});
}
});
Let’s say in the child table there are three variables:
- Total
- Delivered
- Remaining
Remaining = total - delivered
try something like this:
frappe.ui.form.on("Invoice Item", "delivered", function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, 'remaining', total - delivered);});
frappe.ui.form.on("Invoice Item", "total", function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, 'remaining', total - delivered);});
Although I appreciate you trying to help, id appreciate it a bit more if you read my original post. I know how field-based triggers work and yes ideally id write my code with the delivered and total fields as the triggers but i wish to solely use refresh or onload triggers or anything similar for child tables.
you are right, I overlooked that comment in you initial post. sorry!
can you explain why you prefer using refresh/onload?
I am heavily using the field based triggers. do they bring any disadvantage?
thanks!
Not a disadvantage per se, basically I am importing the delivered field from the parent doctype into the child table. Whenever the delivered field is updated in the parent doctype I want it to update in the child table rows too. For ease of use we can say:
- delivered 1 : delivered field in parent doctype.
- delivered 2: delivered field in child doctype.
I want to update delivered 2 whenever delivered 1 is updated. I cannot use delivered 1 as a trigger because it belongs to the parent doctype and not the child doctype
hope I understand correctly.
frappe.ui.form.on("Purchase Order", "delivery_date", function(frm) {
$.each(frm.doc.items, function(i, d) {
d.delivery_date= frm.doc.delivery_date;
}); });
whenever delivery date in the parent doctyp (purchase order) is updated, each delivery_date field on all items is also updated.
This wont work because field-based triggers must be of the doctype for which work is being done. In this case the trigger delivery_date will be the field from the child table.
just to test what @moe01325 has suggested.
in my experience the child table trigger does work within the client scripts of the parent.
Exactly, child table triggers work within the client script for the parents but this scenario is the opposite. We’re trying to use a parent document field trigger in the child table script.
The equivalent of a child table onload would be {fieldname}_on_form_rendered
or form_render
Where fieldname is the table field on the parent, e.g. items
I have used form_render
to retrieve and display data when the child table row is opened with the edit button.
Doc here: Form Scripts
1 Like
Will try and let you know
Cheerio.