I have created a customer form (as shown in the image below). I want to display the value (rate x quanity) in the “Amount” column. Is it possible to this without the access to the erpnext folder as its hosted on frappe cloud.
I don’t know if in frappe cloud you can add client script, but if you can, you can try this.
You will have to modify it with the name of your doctype, your fields, etc.
frappe.ui.form.on('YourDocType', {
refresh(frm) {
// Code that runs when the form is refreshed
}
});
frappe.ui.form.on('Item', { // Replace 'Item' with the actual name of your Child Table DocType
quantity(frm, cdt, cdn) {
// Runs when the "quantity" field in any row of the "items" Child Table changes
calculate_amount(frm, cdt, cdn);
},
rate(frm, cdt, cdn) {
// Runs when the "rate" field in any row of the "items" Child Table changes
calculate_amount(frm, cdt, cdn);
}
});
function calculate_amount(frm, cdt, cdn) {
var child = locals[cdt][cdn];
var amount = child.rate * child.quantity;
// Set the value of the "amount" field in the current row of the Child Table
frappe.model.set_value(cdt, cdn, 'amount', amount);
}
Here is the script:
frappe.ui.form.on(‘Fuel Request Form’, {
refresh(frm) {
// your code here
}
})
frappe.ui.form.on(‘Fuel Request Item’, {
qty(frm, cdt, cdn) {
// Runs when the “quantity” field in any row of the “items” Child Table changes
calculate_amount(frm, cdt, cdn);
},
rate(frm, cdt, cdn) {
// Runs when the “rate” field in any row of the “items” Child Table changes
calculate_amount(frm, cdt, cdn);
}
});
function calculate_amount(frm, cdt, cdn) {
var child = locals[cdt][cdn];
var amount = child.rate * child.qty;
// Set the value of the "amount" field in the current row of the Child Table
frappe.model.set_value(cdt, cdn, 'amount', amount);
function calculate_amount(frm, cdt, cdn) {
var child = locals[cdt][cdn];
var amount = child.rate * child.qty;
// Set the value of the "amount" field in the current row of the Child Table
frappe.model.set_value(cdt, cdn, 'amount', amount);
// Save the entire form to persist the changes
frm.save();
}
I have recreated the information and applied this code to which I have added some checks. Both this code and the previous one have worked perfectly for me.
Refresh the cache, and make sure you don’t have any other code that may be affecting it.
frappe.ui.form.on('Fuel Request Form', {
refresh(frm) {
}
});
frappe.ui.form.on('Fuel Request Item', {
qty(frm, cdt, cdn) {
// Runs when the "quantity" field in any row of the "items" Child Table changes
calculate_amount(frm, cdt, cdn);
},
rate(frm, cdt, cdn) {
// Runs when the "rate" field in any row of the "items" Child Table changes
calculate_amount(frm, cdt, cdn);
}
});
function calculate_amount(frm, cdt, cdn) {
var child = locals[cdt][cdn];
var amount = 0; // Set the initial value to 0
// Check if either the "rate" or "qty" field is equal to 0 before calculating the amount
if (child.rate !== 0 && child.qty !== 0) {
amount = child.rate * child.qty;
}
// Set the value in the "amount" field
frappe.model.set_value(cdt, cdn, 'amount', amount);
}
I found the issue. When I created the child table, I fetched the value for “rate” column from the “item” table. When I removed it, the script worked as intended.