Strange frappe.get_doc behaviour from custom script

Hi guys,

we have a very strange behaviour when running the following custom script (example to demonstrate):

frappe.ui.form.on("Customer", {
   refresh: function(frm) {
      var sinv = frappe.get_doc("Sales Invoice", 'SINV-00001');
      if (sinv != null) {
         frappe.msgprint("Customer: " + sinv.customer);
      }
      else {
	 frappe.msgprint("SINV not found");
      }
   }
});

Expected behaviour: Message displayed with “Customer: (customer name)” whenever a customer record is refreshed.

Actual behaviour: first time a customer record is opened, it correctly displays the message. If you click F5 (refresh), the not found message appears.

Any ideas why? Version is ERPNext: v9.1.5 (master), Frappe Framework: v9.1.7 (master), it reproduces over different installations…

It will fetch he document from the locals in the client side, meaning that it will work unless the document was opened first (in other words, it won’t download the document from the server).

To do so, you may want to use this one instead:

frappe.ui.form.on("Customer", {
	refresh: function(frm) {
		frappe.call({
			"method": "frappe.client.get",
			"args": {
				"doctype": "Sales Invoice",
				"name": "SINV-00001"
			},
			"callback": function(response) {
				var sinv = response.message;

				if (sinv) {
					frappe.msgprint("Customer: " + sinv.customer);
				} else {
					frappe.msgprint("SINV not found");
				}
			}
		});
	}
});
4 Likes

Thanks, this did the trick!