Custom Script to change sales order line amount calculation

I’m on version v10.0.17 and added a custom field on the sales order item doc type.

I have written a custom script that tries to alter the way that the system calculates line total.
By default, the line total is calculated as Qty * Rate.
I would like the line total to be calculated as follows:line total= new_custom * rate.

I have accomplished this successfully using the script below but the changes are discarded when the document is saved to draft.

frappe.ui.form.on(“Sales Order Item”, “new_custom_field”, function(frm, doctype, name) {
var row = locals[doctype][name];

row.amount = row.rate * row. new_custom_field;

The above script works 100% before the document is saved to draft.

What happens is that, as soon as the user saves the document, the system reverts back to line total calculation of Qty*Rate.

I would really appreciate it someone could assist with a solution or possible workaround.

Thanks in advance.

@Eli, you cannot do this, because the backend ensure the values, so if you try, it dont work!

But what you can do, is use your custom field, to re-set the quantity field.
So, you can mimic the behavior.

@max_morais_dmm - Thanks for the clarity.It’s very much appreciated.

How would the re-set take place?Through the back end or via another custom script?

@max_morais_dmm After giving your response much thought, I have made sense of what you are saying.
I have managed to reset the both the qty and rate field to come back to the result I was after by the initial rate * custom field.

I’ve had to add more custom fields to keep the original values in the qty and rate so that these can be displayed to the customer.

In the back end however, to keep things simple, I reset the original qty and rate which I just hide when printing documents.

Thanks for nudging me in the right direction.

Very much appreciate the assistance