Fetch Price from Price List.js code need some improvement

Dear Sir,

I have a question and a problem.

In my custom doctype, in the child table, i have created item_price atribute that i need to fetch the values from the item pricelist.

I have created a .js file to fetch data values based on the item_code to fetch price list from the item.

this is my script:
frappe.ui.form.on(“Est Tools Item”, “item_code”, function(frm, cdt, cdn) {
row = locals[cdt][cdn];
frappe.call({
method: “frappe.client.get”,
args: {
doctype: “Item Price”,
filters: {
“item_code”: row.item_code,
“price_list”: “Standard Selling”
}
},
callback: function (data) {
frappe.model.set_value(cdt, cdn, “price_list_rate”, data.message.price_list_rate);
}
})
});

The script work in fetching the item price from the designated pricelist for the specific item code.

The question is:

  1. When the price list are there, the price list are fetched without no problem.
  2. When an item doesnt have any price list available yet, the script created an error that stated bellow:
    ( i beleive the meaning is that "you dont have any price list for this item’)

what i would like is, "when there is no price list on the item, instead of showing error, the script just have to return “0” in the item price box.

I appreciate your help and I am sorry to ask this.
Thanks

Anton

this is the error that created:
Uploading error.jpg…

I can’t see the error - could you try reposting it or copy/pasting?

Assuming it’s an exception for not found thrown on the server-side, try this…

frappe.ui.form.on("Est Tools Item", "item_code", function(frm, cdt, cdn) {
    row = locals[cdt][cdn];
    frappe.call({
        method: "frappe.client.get_value",
        args: {
            doctype: "Item Price",
            fieldname: "price_list_rate",
            filters: {
            "item_code": row.item_code,
            "price_list": "Standard Selling"
            }
        },
        callback: function (data) {
            frappe.model.set_value(cdt, cdn, "price_list_rate", data.message); //might need to be data.message[0]
        }
    })
});

If that doesn’t work, you can put the whole thing in a try/catch block, but I only recommend doing this if the above doesn’t work.

frappe.ui.form.on("Est Tools Item", "item_code", function(frm, cdt, cdn) {
    row = locals[cdt][cdn];
    try {
        frappe.call({
            method: "frappe.client.get",
            args: {
                doctype: "Item Price",
                filters: {
                "item_code": row.item_code,
                "price_list": "Standard Selling"
                }
            },
            callback: function (data) {
                frappe.model.set_value(cdt, cdn, "price_list_rate", data.message.price_list_rate);
            }
        })
    catch (err) {
        console.log(err) // Log the error in the browser console
        row.price_list_rate = 0
        cur_frm.refresh();
    }
});

@alec_ruizramon1

Dear Sir,
The code you give me it Works!

I dont see any error anymore,

Thank you !

Anton

You’re welcome! Glad to help. :smile:

For future reference, frappe.client.get throws an error if no doc is found, but frappe.client.get_value returns None as a value if none is found.

2 Likes

@tara_antonius, @alec_ruizramon1
I can’t seem to get the revised version of this script to work for me.

I CAN get the original script to work and pull the price from the price list, and it has the error message as described if there is no price in the list, but the revised version does not work for me (no error, and no price pulled). Any thoughts?

This script works:
frappe.ui.form.on(“Opportunity Item”, “item_code”, function(frm, cdt, cdn) {
row = locals[cdt][cdn];
frappe.call({
method: “frappe.client.get”,
args: {
doctype: “Item Price”,
filters: {
“item_code”: row.item_code,
“price_list”: “Standard Selling”
}
},
callback: function (data) {
frappe.model.set_value(cdt, cdn, “rate”, data.message.price_list_rate);
}
})
});

This script does not work:
frappe.ui.form.on(“Opportunity Item”, “item_code”, function(frm, cdt, cdn) {
row = locals[cdt][cdn];
frappe.call({
method: “frappe.client.get_value”,
args: {
doctype: “Item Price”,
fieldname: “price_list_rate”,
filters: {
“item_code”: row.item_code,
“price_list”: “Standard Selling”
}
},
callback: function (data) {
frappe.model.set_value(cdt, cdn, “rate”, data.message);
}
})
});

@Dbone
have you calculated with quantity?.