Server Script Not working Child Table

My server script is not working for a child doctype. Script works fine when I remove Child Property from doctype. My script is as follows:

doc.amount=doc.hours*doc.hourly_rate

// amount is a currency field in doctype
// hours is a float field in doctype
// hourly_rate is a currency field in doctype

I tried adding the script to the Parent instead of the child but it gave me the following error :
“TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘NoneType’”

Usually we put the code in the parent doctype, not the child doctype. But it’s usually something like this:

for item in doc.items: # items is the field name of the child table/doctype;
     doc.amount += item.hours*doc.hourly_rate

@littlehera
I wrote the server script in the parent doctype, changed the field name of the child table to item. It now gives this error upon saving :
“SyntaxError: (‘Line 2: Augmented assignment of attributes is not allowed.’,)”

my code is the exact same as yours.

Instead of using +=, try adding it otherwise.

@rtdany10
Done that. My syntax was as follows:
‘for item in doc.items:
doc.amount = doc.amount + (item.hours*doc.hourly_rate)’

// error upon saving parent doctype:“TypeError: ‘NoneType’ object is not iterable”

I also tried
for item in doc.items: doc.amount = doc.amount + (item.hours*item.hourly_rate)
// as both amount and hourly_rate are fields of the child table, I thought perhaps this would do the trick but this is the error:
“TypeError: ‘NoneType’ object is not iterable”

@littlehera
also shouldnt the code be :
for item in doc.items: # items is the field name of the child table/doctype;
doc.amount += item.hours*item.hourly_rate

//item.hourly_rate instead of doc.hourly_rate as hourly_rate is a field of the child doctype?

I tried this but got the same syntax error.

Furthermore by looking at the code i’m assuming you think that hourly_rate and amount are fields of the parent doctype. They are not, all three fields belong to the child doctype and the calculated amount is for each entry in the table.

For whatever is in the parent, use doc suffix.
For fields in the child table, use item suffix.

Understood your error. There is no field named items(doc.items) in your doctype. Go to your doctype and find the name of the table. And iterate over it.

I did that. The first thing i did was to change the name of the table field to items. Same error

There is another issue however, I was able to work around the amount calculator for the child doctype with the following code

for item in doc.items: # items is the field name of the child table/doctype;
item.amount = item.hours*item.hourly_rate

However this nulls my js code for the parent doctype which is :

frappe.ui.form.on(“PM Invoice”, {
amount:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount; });
frm.set_value(“grand_total”, total);
refresh_field(“grand_total”);
},
pminvoice_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount; });
frm.set_value(“grand_total”, total);
refresh_field(“grand_total”);
}
});

where PM Invoice is the table’s name. grand_total is a field in the parent doctype which is the sum of all amounts from the table. Now the grand_total only shows up as zero

Why don’t you calculate the grand_total on validate?
Also, that variable definition of d is unnecessary.

I worked a fix for all of this.

  1. To calculate individual total for each table entry :
    for item in doc.items:
    item.amount = item.hours*item.hourly_rate

  2. For sum of amount entries from whole table to parent doctype:
    for item in doc.items: # items is the field name of the child table/doctype;
    doc.grand_total_test = doc.grand_total_test + item.amount

where grand_total_test is the fieldname of the sum of all amounts from doctype. Note I did create two separate scripts for each function, both for the same doctype.

Thanks @littlehera, @rtdany10 for your support. Many new developers might benefit from this. :smiley:

2 Likes