Problem using frappe.model.get

Hi,

I’m struggling to customize frappe.
I the previous version of erpnext (v3) I had a little script to extract the Sales order and PO number from the individual items in a Sales Invoice.

However, the script fails and the browser reports:

typeError: ‘undefined’ is not a function (evaluating ‘frappe.model.get(“Sales Invoice Item”, {parent: doc.name})’)

Any ideas on how to use frappe.model.get correctly?

Br
Jens Bohlin

// Script to set the two custom fields:
// Sales Order (agg_so) and
// Customer’s Purchase Order Number (agg_po)
// Note: Sales Order name may not contain “,”. (Normally not a problem)

cur_frm.cscript.custom_onload = function(doc) {
var so_ref = “”;
var po_ref = “”;

$.each(frappe.model.get(“Sales Invoice Item”, {parent: doc.name}), function(i, d) {
if (d.sales_order) {
if (so_ref.indexOf(d.sales_order) == -1) {
if (so_ref) {
so_ref += ', ';
}
so_ref += d.sales_order;
cur_frm.set_value(“agg_so”, so_ref);

  		frappe.call({
  			method: "frappe.client.get_value",
  			args: {
  				doctype: "Sales Order",
  				filters: {
  					name: d.sales_order
  				},
  				fieldname: ["po_no"]
  			},
  			callback: function(r) {
  				console.log(r);
  				if (po_ref) {
  					po_ref += ', ';
  				}
  				po_ref += r.message.po_no;
  				cur_frm.set_value("agg_po", po_ref);
  			}
  		});
  	}
  }

});
}

// Update also when page is refreshed…
cur_frm.cscript.custom_refresh = cur_frm.cscript.custom_onload;

Just use to loop over child items.

$.each(doc.entries, function(i, d) { });

Great! That worked!
Where can I find this kind of documentation?
All your client side scripts examples suggest using $.each(wn.model.get(

Br
Jens

API changed in Version4 - which docs are you referring?

Hi,

I was refering to the inline examples in Custom Script Page on frappecloud.

Br
Jens Bohlin

1 Like

Thanks fixed. Will go in v5 though :smile: