Can Custom Scripts fetch field values from linked objects at creation time?

Hi All,

I have a Customer and Sales Invoice Doctype, both with a custom field called tax_id. When I go to a Sales Order under that customer, and Make Invoice, I want the newly created (and currently unsaved) Sales Invoice to have its Tax ID field populated with the value from the linked Customer.

The following custom script works when I edit or change the linked Customer on the Sales Invoice. But not when the Sales Invoice is first loaded.

cur_frm.add_fetch("customer", "tax_id", "tax_id");

Is there a javascript handler that allows me to call add_fetch() after my new Sales Invoice loads?

I recall that I ran into this same issue a couple years ago, and I ended up patching the python code.

Any tips greatly appreciated.
Jev

@ebjorsell

frappe.ui.form.on("Sales Invoice", "onload", function(frm, cdt, cnd){
  if (frm.doc.__islocal && frm.doc.customer){
    frappe.db.get_value("Customer", frm.doc.customer, "tax_id", function(res){
      frm.set_value("tax_id", res.message.tax_id);
    });
  }
});

Edit after a note in gitter:

“”"
@MaxMorais thanks for your reply on discourse Your solution doesn’t work with my version though. Tested on v6.12.3 and the get_value method is not defined. v6.27.21 is also throwing an error about frm is not definedwhich seems a bit odd.
“”"

frappe.ui.form.on("Sales Invoice", "onload", function(frm, cdt, cdn){
  if (cur_frm.doc.__islocal && cur_frm.doc.customer){
    frappe.call({
      "method": "frappe.client.get_value",
      "args": {
         "doctype": "Customer",
         "filters": cur_frm.doc.customer,
         "fieldname": "tax_id"
       },
      "callback": function(res){
        if (!res.exc){
          cur_frm.set_value("tax_id", res.message.tax_id);
        }
      }
    });
  }
});

3 Likes

@MaxMorais thanks for your reply!

I tested your first solution, but it didn’t work on v6.12.3. The get_value method is not defined.
I also tested on v6.27.21, and that version is giving a different error about frm is not defined which seems a bit odd.

Your second solution works great on both versions. Thank you very much! :slight_smile:
Jev

1 Like