Button on parent doctype to trigger changes in child doctype

Hello,

My use case is that I want to calculate a selling price based on the shipping terms (exworks, CIP, DDP) for each item in my items table in quotation. And I do that successfully for each item using the following code. The script is linked to Quotation doctype.

function calculateSellingPrice(exworks, freight, shippingTerms) {
  var cip = (exworks * 1.01) + freight;
  var ddp = cip * 1.05;
  if (shippingTerms == "EXWORKS") {
    return exworks;
  } else if (shippingTerms == "CIP") {
    return cip;
  } else {
    return ddp;
  }
}


frappe.ui.form.on("Quotation Item", "qty", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

frappe.ui.form.on("Quotation Item", "price_list_rate", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

frappe.ui.form.on("Quotation Item", "discount_percentage", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

frappe.ui.form.on("Quotation Item", "total_margin", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

frappe.ui.form.on("Quotation Item", "rate", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

frappe.ui.form.on("Quotation Item", "freight", function(frm,cdt,cdn) {
  var d = locals[cdt][cdn];
  var sellingPrice = calculateSellingPrice(d.rate, d.freight, frm.doc.shipping_terms);
  frappe.model.set_value(d.doctype, d.name, "selling_price", sellingPrice);
});

Now, I want the calculation to trigger also when I press a button on the parent doctype (quotation), or every time the grand_total field updates.

I have been searching in the form a lot but didn’t find something that helped with this one.
Looking forward for the forum’s support.
By the way, this is my first script in erpnext, and my first attempt with javascript, so please keep it simple and clear.

Regards,
Ahmed

Any help out there please?

So you are saying, this code is not working for you?

Dear @KanchanChauhan, the cord works for events generated in the Child Doctype (or child table) only. But I couldn’t write code that will take an event in the parent doctype and update a field in the child doctype.

Regards,
Ahmed

There is no reason you cannot update the child doctype. Just set the correct value in frm.doc and then use frm.refresh