How to fetch value from Item->opening_stock in Client Script

Hello,

I have added a field Item to Mfg in my custom app to a DocType Route Card. This is a read only data field and it gets populated when a user selects a Work Order.

Here I want to fetch the Item’s opening_stock value in a field called opening_stock in the DocType Route Card.

Can I do this only from client script or will I have to write server script?

TIA

Yogi Yang

@YogiYang You can do that using a Client Script. If you want, post a screenshot of the fields of all the involved doctypes so I can help you with the code.

Hello,

Here is the screen shot:


Currently I have added a random value of 120 to the field.

The field names are:
my_work_order_num When user selects a Work Order here it should fetch Opening Stock from Item DocType.

item_to_mfg This field is populated when user selects Work Order in my_work_order_num field.

op_stock This is the field where the fetched value has to be populated.

The DocType is My Route Card

TIA

Yogi Yang

@YogiYang The script you can use is:

frappe.ui.form.on('My Route Card', {
    my_work_order_num: function(frm) {
        if (!frm.doc.my_work_order_num) {
            frm.set_value({
                'item_to_mfg': '',
                'op_stock': '',
            });
            return;
        }
        frappe.db.get_value('Work Order', frm.doc.my_work_order_num, 'production_item')
        .then(wo => {
            let production_item = wo.message.production_item;
            frm.set_value('item_to_mfg', production_item);
            frappe.db.get_value('Item', production_item, 'opening_stock')
            .then(it => {
                let opening_stock = it.message.opening_stock;
                frm.set_value('op_stock', opening_stock);
            });
        });
    }
});

What will happen is:

  • If my_work_order_num is empty, both the item_to_mfg and op_stock will be empty
  • If my_work_order_num is not empty, then the item_to_mfg will be filled with production_item from Work Order doctype and op_stock will be filled with the opening_stock of the production_item from Item doctype
1 Like