Fetching item prices in Opportunity

Hello, I was wondering if it is by design or maybe I could not have found the option to automatically fetch price from price list for items in Opportunity items section. Any help would be appreciated.

Hi @Leviatanus,

First, create a custom field like Price List in Opportunity DocType.

Then apply the client script.

frappe.ui.form.on('Opportunity Item', {
    item_code: function(frm, cdt, cdn) {
        var item = frappe.get_doc(cdt, cdn);
        var price_list = frm.doc.price_list; // Get the selected price list from the Opportunity DocType
        var item_code = item.item_code;

        frappe.call({
            method: 'frappe.client.get_value',
            args: {
                doctype: 'Item Price',
                filters: {
                    item_code: item_code,
                    price_list: price_list
                },
                fieldname: 'price_list_rate'
            },
            callback: function(response) {
                if (response && response.message) {
                    frappe.model.set_value(cdt, cdn, 'rate', response.message.price_list_rate);
                }
            }
        });
    }
});

Only worked if the Item will have a one-price list rate.

Item Price:

Output:

First, select the Price List, then after, select the Item code.
Item Rate will appear.

I hope this helps.
Thank You!

1 Like

Thank you very much for the detailed reply! I have successfully applied the script in my system.

Firstly, I have added the Price List field:

Below, modified code with additional check for currencies - if the currency of Price List is different from that of Opportunity the script will display an error. Additionally, the scrip displays warning if the Price List has not been set.

frappe.ui.form.on('Opportunity Item', {
    item_code: function (frm, cdt, cdn) {
        // This script requires Price List field to be added to Opportunity
        
        var item = frappe.get_doc(cdt, cdn);
        var price_list = frm.doc.price_list; // Get the selected price list from the Opportunity DocType
        var item_code = item.item_code;

        var currency = frm.doc.currency;

        if (!price_list) {
            frappe.msgprint(`Information: Price List has not been set`);
            return;
        }

        // Get currency of the price list
        frappe.call({
            method: 'frappe.client.get_value',
            args: {
                doctype: 'Price List',
                filters: { name: price_list },
                fieldname: 'currency'
            },
            callback: function (response) {
                var price_list_currency = response.message.currency;
                // Get item price
                frappe.call({
                    method: 'frappe.client.get_value',
                    args: {
                        doctype: 'Item Price',
                        filters: {
                            item_code: item_code,
                            price_list: price_list
                        },
                        fieldname: 'price_list_rate'
                    },
                    callback: function (response) {
                        if (response && response.message) {
                            // if the currency of Opportunity and Price List is the same, set the item rate
                            if (currency === price_list_currency) {
                                frappe.model.set_value(cdt, cdn, 'rate', response.message.price_list_rate);
                            } else {
                                frappe.msgprint(`The currency of Opportunity (${currency}) must be the same as Price List currency (Currency of ${price_list}: ${price_list_currency})`);
                            }
                        }
                    }
                });
            }
        });
    }
});

Once again, thank you for your help! The solution works well for the client.