How to manipulate values of a specific Doctype form field based off a formula?

I created a client script for the Doctype form : Promos to calculate a total discount amount.

However, upon enabling this script it won’t change the value. Can someone help how to manipulate form field values?

frappe.ui.form.on('Promos', {
    validate: function(frm){
        var discount_rate = frm.doc.discount_rate;
        var srp = frm.doc.suggested_retail_price_srp;

        // Calculate total discount value
        var total_discount = srp * (discount_rate / 100);

        // Set the calculated value back into the discount_value field
        frm.set_value('discount_value', total_discount);
    }
})

Works fine for me.™

Can you describe what steps you’ve taken to troubleshoot?

The values wont log to the console.

What values? There are no console.log statements in what you’ve shared.

console.log(“Total discount”, total_discount);

Okay, what else have you tried? Do console.log statements outside the validate method work?

The more troubleshooting you can do on your own, the more we’ll be able to help you. At the moment, it’s pretty impossible to say anything more than “It works for me”.

Just needed to use a function for the formula.

1 Like