Get Total Amount from Sales Opportunity - With Items

Hello,

I found this below script on the discussion board

frappe.ui.form.on("Opportunity Item", "qty", function(frm, cdt, cdn){
    var d = locals[cdt][cdn];
    frappe.model.set_value(cdt, cdn, "amount", d.qty * d.amount);
});

that I inserted into a custom script with “Opportunity” doctype, but it didn’t work.
I customized a field on Opportunity form by creating “Amount” field, but it also didn’t work.
I would like to get the total amount of “With Items”, how can I do?

Thanks in advance.
Best Regards,
Mic

It will be triggered by a change of qty field.

?
amount = qty * amount

better way you can take rate field, then you can use,

frappe.model.set_value(cdt, cdn, "amount", d.qty * d.rate);

Hello,

Thanks so much for your reply.
The Qty field is not replaced as you mentioned. I’m not sure how the code works. Can you suggest me more information?

Thanks

you have Qty, Rate, Amount fields, right?
So your amount will be - qty * rate

As per this code, it will work when you change the qty.

Try this way.

In addition, I’haven’t found the field “rate” and also the previous one “amount” that I used in the Opportunity’s field structure. Any ideas?

Thanks

Rate and Amount fields are not available in default one you can add it using a custom field. But I think that’s not required for Opportunity. These fields are available in further selling and buying forms.

Thanks Sangram,

However, in case it is not available as in default, so how can I get an item rate from its selling rate or from a price list in order to get it to calculate with Qty in Opportunity? Because the objective by doing so, I’d like to get a total selling price (Qty * item amount) of the opportunity item table. How can I achieve this?

Thanks so much in advance.

Regards,

@cipher If you want to calculate total amount from item child table check with

frappe.ui.form.on(“Opportunity Item”,“qty”, function(frm, cdt, cdn) {
var row = locals[cdt][cdn]
frappe.model.set_value(row.doctype, row.docname, “amount”, d.qty * d.rate);

});

after setting amount to each row in opportunity item count your total amount

$.each(cur_frm.doc.opportunity_iitem, function(i,v){
//your calculation
})

In i miss out some speeling try out with this

frappe.ui.form.on(“Opportunity Item”,“qty”, function(frm, cdt, cdn) {
var row = locals[cdt][cdn]
frappe.model.set_value(row.doctype, row.docname, “amount”, row.qty * row.rate);

});

Hello Khushal_t,
Thanks so much for your reply. Your solution doesn’t work and I got some error raising when open Opportunity page. Could you please step into the solution in detail, how can I implement your suggestion? I’m quite new to ERPNext, so I’m probably implementing incorrectly. Additionally, I’m wondering that in Opportunity Items, there is no “rate” or “amount” in order to get for calculation, so how does it work for the solution?

Regards,

@cipher Please you check console for errors please check that in for loop you have correct child table name . what I got is that you want to calculate the total amount in your opportunity item table eg; if it has 5 rows each have 100 amount so you want final amount to be 500 so if this is correct what I have got then
1)firstly calculate the amount for each row that is if you write a trigger on your qty so you will calculate row.qty * row.amount to that row so that u will set amount

> frappe.ui.form.on("<Child doctype Name>","<field(on which u want to trigger)>", function(frm, cdt, cdn) {
> 	var row = locals[cdt][cdn]
> 	frappe.model.set_value(row.doctype, row.name, "<Field on which u want to set the value>", row.qty * row.amount);
> 
> });
  1. after calculating to each row now you want to calculate total value for that child table in your case guess Opportunity Item

$.each(cur_frm.doc.opportunity_item, function(i,v){
//Here you iterate on your table to claculate

})

don’t directly copy and paste, check your field name for the same if I have understood your scenario well this should probably work. if I am wrong please provide more details

Hello Khushal_t,
Thank you so much for your reply. Please help me to check this link https://erpnext.org/docs/current/models/crm/opportunity_item?
There is no “Amount” or “Rate” field that involves an item rate (Selling price). Is it possible to use
row.qty * row.amount
because I still don’t get that where I can get an item price for calculation. And what do you mean by
//Here you iterate on your table to claculate
What syntax do I need to insert here? Feel sorry that I don’t understand what I have to do next really on this line.

Regards,

@Sangram

Can we fetch item price from selling price list rate in opportunity item table?. As same as quotation item table.

Thanks,
Manju