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!
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.
This has to be a bug, no? Any time you reference an item it should bring in the rate/price of that item. I can’t believe NOT doing that is by design. It doesn’t make sense. Why would I need to open another tab to look up the price only to enter it manually?
By default, the Opportunity doctype doesn’t offer the price list rate; instead, users must enter the rate manually. However, if you wish to set the price list rate, you can configure it as I described in the above post. Configuring this scenario is straightforward.
If it’s straightforward, Development should put it in the product. That’s a pretty big oversight, in my opinion.
Price list rate isn’t set in the Opportunity doctype because it’s designed to focus on tracking potential sales opportunities rather than specific pricing details.
When you’re dealing with opportunities, you’re mainly trying to figure out if a potential customer is interested in your product or service. The actual price they might pay isn’t usually discussed at this stage.
The pricing details typically come into play later, once the opportunity is converted into a quotation or sales order. That’s when you decide on the specific price the customer will pay, based on factors like quantity, discounts, and any special agreements.
So, in simple terms, the Opportunity doctype is about identifying sales possibilities, while pricing details are sorted out in later stages when converting the opportunity into an actual sale (quotation/sales order).
I understand what you are saying.