Custom Script for Child Table doesn't work

Hi All,

I have created a custom script that runs everytime there is a new item added to the child table. My issue is that when an item is initially added some of the custom fields doesn’t get filled up. Looks like the issue is that I’m passing a variable that doesn’t exist yet. See below:

image

My custom script is the one that starts with erpnext_core_customizations. It requires the warehouse from the item but that data only exist when the erpnext.stock.get_item_details.get_item_details gets called. I’m just wondering if there is a way for me to write my script to wait for all calls before running my custom script.

My client-side script is below:

frappe.ui.form.on(‘Quotation Item’, {
item_code(frm,cdt,cdn) {
per_line_cost(frm,cdt,cdn);
}});

function per_line_cost(frm,cdt,cdn){
	var d = locals[cdt][cdn];
	var total_bom_cost = 0.0;
	var last_purchase_cost = 0.0;
	var valuation_rate_cost = 0.0;
	var total_override_cost = 0.0;
	d.total_bom_cost = total_bom_cost;
	d.last_purchase_cost = last_purchase_cost;
	d.total_valuation_rate_cost = valuation_rate_cost;
	if (d.override_cost === 1){
		total_override_cost += d.per_item_cost * d.qty;
		d.override_total_cost = total_override_cost;
	}
	frappe.call({
		method:'frappe.client.get_value',
		args: {
		'doctype': 'Item',
		'filters': {'name': d.item_code},
		'fieldname':[
			'item_name',
			'last_purchase_rate',
			'default_bom'
			]},
		callback:function(r){
		if (!r.exc){
		d.item_last_purchase_rate = flt(r.message.last_purchase_rate);
		last_purchase_cost += flt(d.item_last_purchase_rate * d.qty);
		d.total_last_purchase_cost = last_purchase_cost;
		frappe.call({
			method: 'erpnext_core_customizations.erpnext_core_customizations.doctype.quotation_custom.quotation_custom.get_valuation_rate',
			args: {'item_code': d.item_code,
			'warehouse': d.warehouse
		},
		callback:function(b){
		if(!b.exc){
		d.item_current_valuation_rate = flt(b.message[0]);
		valuation_rate_cost += flt(d.item_current_valuation_rate * d.qty);       
		d.total_valuation_rate_cost = valuation_rate_cost;
		if (r.message.default_bom){
		frappe.call({
			method: 'frappe.client.get_value',
			args:{
				'doctype': 'BOM',
				'filters': {'name': r.message.default_bom},
				'fieldname':[
					'name',
					'total_cost']
			},
			callback:function(a){
			if(!a.exc){
			d.bom_cost = flt(a.message.total_cost);
			total_bom_cost += flt(d.bom_cost * d.qty);
			d.total_bom_cost = total_bom_cost;
			}
			}
		});
		}
	}        
	
}
});
}
}
});
}'

Server side Script:

from future import unicode_literals
import frappe
from frappe.utils.data import flt
from frappe.model.document import Document

@frappe.whitelist()
def get_valuation_rate(item_code, warehouse = None):
    last_valuation_rate = frappe.db.sql("""
        SELECT valuation_rate FROM `tabStock Ledger Entry` WHERE item_code = %s
        AND warehouse = %s AND valuation_rate > 0
        ORDER BY posting_date DESC, posting_time DESC, creation DESC LIMIT 1
        """, (item_code, warehouse))
    if last_valuation_rate:
        return last_valuation_rate[0]
    return 0