Need help for loading item details

hello,

i have made some custom fields on “Sales Order Item”,

i would like to calculate item price and set value to custom fields.

i wrote the following custom script to “Sales Order”

frappe.ui.form.on(“Sales Order Item”, {

item_code: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.my_custom_field = (row.rate * row.qty) + frm.doc.speical_charge;
refresh_fields(“items”);
}
})

but the code fails, row.my_custom_field is still 0.

it seems my code is running before loading of item details, so my result is still zero.

if i trigger “qty” instead of “item_code” it does work.

how to solve this problem to trigger with “item_code”?

please help, thank you.

Your code will run after had changed on item_code field
And once changing item code and select the item, the rate and quantity of this item will, of course, be 0
So you need to change this trigger to handle rate and qty changing

frappe.ui.form.on(“Sales Order Item”, {

rate: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.my_custom_field = (row.rate * row.qty) + frm.doc.speical_charge;
refresh_fields(“items”);
},

qty: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
row.my_custom_field = (row.rate * row.qty) + frm.doc.speical_charge;
refresh_fields(“items”);
}
})

hello,

thank you for your reply.

yes, my code will run after item_code get value, but fails to get row.rate.

i think system default is also triggering item_code field for loading item details (such as item name, description, price) into child table. but my code is fired a bit earlier (or at same time with system triggering item_code changing) so rate and qty field have not been fetch yet.

i would like my script will get fired on item_code, but after system fetching item details to ensure rate and qty value are available.

please help, thank you.

Hi,
can you please post a screenshot of your child and parent doctypes with your “custom fields added” so i can see the fields you want to use. If i understand you correctly, you need to first save then calculate so everything is fetched and the correct values are set. you can save first and then trigger a recalculation of your table upon saving.

Hi cupuno,

I’m facing the same problem. How did you solve it?

Thank you!