How to add/multiply values and display them

Hello everyone,

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.

Here are the system details:
ERPNext: v15.10.5 (HEAD)
Frappe Framework: v15.10.0 (HEAD)

Thanks,
Deepak

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);
}

1 Like

Thank you very much for this information @jls .

I would also add some field values validation to avoid any exceptions during math calculations.

1 Like

Hello @jls ,

The script worked. The amount is getting calculated perfectly. But the value in the “rate” column is refreshed to zero when I save the form.


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);

}

How can I fix this?

Thanks,
Deepak

Try this

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();
}
1 Like

Hi @jls

The “rate” column is still showing as zero value after automatically saved.

I will check it later.

@deepak19 can you create server script in frappe cloud? I didnt remember.

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);
}

2 Likes

Hello @jls ,

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.


It was clear that there was something more.

I’m glad it works. Since the script has been useful to you, mark it as a solution so that it can be useful to others.

1 Like