Server Script for custom fields in Sales Invoice

Disclaimer: This is my first step in server-side scripting

I have added a custom field in Item Price and two custom fields in Sales Invoice in the items-child table.

The fields are called rr and rr_amount. The value of rr should come from the used Item Price and should be multiplied by the charged qty to automatically fill in rr_amount

The next step that I need to take is to add all rr_amount-fields on a Sales Invoice and put the sum into another custom field on the main DocType Sales Invoice called rr_grand_total But before I can start to figure out this I need to get the script working. I’ve already spent a lot of hours trying to fix this by using a Client Script, before finding out that the behavior I’m looking for should be handled by a Server Script.

I’ve managed to get the Server Scripts active on our ERPNext v13 setup so I could start writing my first Server Script:

This is what I have now:

Script Type: DocType Event
Reference Document Type: Sales Invoice
DocType Event: Before Save

def calculate_child_table_values(doc, method):
    for row in doc.items:
        row.rr_amount = row.qty * row.rr

and the script shows enabled.

I would not be here if this was working so nothing happens at all in the rr_amount-field.

Please check if the field rr is pulling value from the Item Price, its possible that it is not and hence this code / calculation is failing.


Thank you Pawan for the insight. It seems true that the field is not pulled from the Item Price. I’ve been messing around with adding the value manually later on but that would not help as I presume the calculation should be done when the fields are populated.

I’ll try to figure out why the field is not pulled although they both have the same name. I see the same issue happening in my Sales Order where I also have the rr field. When adding a new item to a Sales Order the rr-field stays empty.