Fetch from in Dialogue

frappe.ui.form.on("Appraisal", {
    refresh(frm) {
        frm.add_custom_button(__("Training Needs"), function () {
            let d = new frappe.ui.Dialog({
                title: 'Enter details',
                fields: [
                    {
                        label: 'Employee',
                        fieldname: 'employee',
                        fieldtype: 'Link',
                        options: 'Employee'
                    },
                    {
                        label: 'Employee Name',
                        fieldname: 'employee_name',
                        fieldtype: 'Data',
                       fetch_from: "employee.employee_name",
                        read_only: 1
                    },
                    {
                        label: 'Training Need',
                        fieldname: 'training_need',
                        fieldtype: 'Text'
                    }
                ],
                size: 'small', // small, large, extra-large 
                primary_action_label: 'Submit',
                primary_action(values) {
                    console.log(values);
                    d.hide();
                }
            });
            
            d.show();            

        }, __("Create"));
    },
});

When i try to add fetch from in dialgoue but it is not working ??

Reference:

2 Likes

Thanks @NCP Buddy

frappe.ui.form.on("Appraisal", {
    refresh(frm) {
        frm.add_custom_button(__("Training Needs"), function () {
            let d = new frappe.ui.Dialog({
                title: 'Enter details',
                fields: [
                    {
                        label: 'Employee',
                        fieldname: 'employee',
                        fieldtype: 'Link',
                        options: 'Employee',
                        data: frm.doc.employee,
                        onchange: function() {
                            let employee_value = d.get_value('employee');
                            if (employee_value) {
                                frappe.call({
                                    method: 'frappe.client.get_value',
                                    args: {
                                        doctype: 'Employee',
                                        filters: { name: employee_value },
                                        fieldname: ['employee_name']
                                    },
                                    callback: function(response) {
                                        d.set_value('employee_name', response.message.employee_name);
                                    }
                                });
                            }
                        }
                    },
                    {
                        label: 'Employee Name',
                        fieldname: 'employee_name',
                        fieldtype: 'Data',
                        read_only: 1,
                        depends_on: 'employee'
                    },
                    {
                        label: 'Training Need',
                        fieldname: 'training_need',
                        fieldtype: 'Text'
                    }
                ],
                size: 'small', // small, large, extra-large 
                primary_action_label: 'Submit',
                primary_action(values) {
                    console.log(values);
                    d.hide();
                }
            });
            
            d.show();            

        }, __("Create"));
    },
});

Is this code is correct for auto populate the employee from doctype Appraisal in the dialogue like data: frm.doc.employee ??

Looks perfect, but test it :wink:


Not auto populate the employee from Appraisal in the dialogue?

Code tested, it’s worked properly.

Yes that code work fine but i want another requirement like I want to auto populate the data of employee from doctype Appraisal instead selecting a employee in the dialogue. So when dialogue open then it should already come with the data present in the Appraisal so i write data: frm.doc.employee, this it is correct ?

 {
                        label: 'Employee',
                        fieldname: 'employee',
                        fieldtype: 'Link',
                        options: 'Employee',
                        data: frm.doc.employee,
                        onchange: function() {
                            let employee_value = d.get_value('employee');
                            if (employee_value) {
                                frappe.call({
                                    method: 'frappe.client.get_value',
                                    args: {
                                        doctype: 'Employee',
                                        filters: { name: employee_value },
                                        fieldname: ['employee_name']
                                    },
                                    callback: function(response) {
                                        d.set_value('employee_name', response.message.employee_name);
                                    }
                                });
                            }
                        }
                    },

Okay :thinking:

You have to set the default.

Please check it.

1 Like

Yes it is working :innocent:

Just want to clear the doubt :

  1. For auto populate the data in Child Table we use data as an argument:
let dialog = new frappe.ui.Dialog({
                            title: 'Stock Balance',
                            size: "extra-large",
                            fields: [{
                                label: 'items',
                                fieldname: 'table',
                                fieldtype: 'Table',
                                read_only: 1,
                                cannot_add_rows: true,
                                in_place_edit: true,
                                data: r.message,
                                fields: [{
                                        fieldname: 'item_code',
                                        fieldtype: 'Data',
                                        in_list_view: 1,
                                        label: 'Item Code',
                                        read_only: 1
                                    },
                                    {
                                        fieldname: 'warehouse',
                                        fieldtype: 'Data',
                                        in_list_view: 1,
                                        label: 'Warehouse',
                                        read_only: 1
                                    },
                                    {
                                        fieldname: 'actual_qty',
                                        fieldtype: 'Data',
                                        in_list_view: 1,
                                        label: 'Qty',
                                        read_only: 1
                                    }
                                ]
                            }],
                        });
  1. For Normal field we use default as an argument:

Like you showed in the above example ??

possible, but you have to set a some logic, it’s not a easily achieve that :sweat_smile:

1 Like

Okay ,Thanks a lot for your help!

Hi @Rehan_Ansari,

Please check the syntax code.

Here i add populate the Delivery Note Item Stock in the Dialog, so please check it.

frappe.ui.form.on('Delivery Note', {
    refresh: function(frm) {
        frm.add_custom_button(__('View Stock'), function() {
            let items = frm.doc.items;

            let data = items.map(item => ({
                item_code: item.item_code,
                warehouse: item.warehouse,
                actual_qty: item.actual_qty
            }));

            let fields = [{
                fieldname: 'items_table',
                fieldtype: 'Table',
                cannot_add_rows: true,
                cannot_delete_rows: true,
                in_place_edit: true,
                fields: [{
                    fieldname: 'item_code',
                    fieldtype: 'Data',
                    in_list_view: 1,
                    label: 'Item Code',
                    read_only: 1
                },
                {
                    fieldname: 'warehouse',
                    fieldtype: 'Data',
                    in_list_view: 1,
                    label: 'Warehouse',
                    read_only: 1
                },
                {
                    fieldname: 'actual_qty',
                    fieldtype: 'Data',
                    in_list_view: 1,
                    label: 'Qty',
                    read_only: 1
                }],
                data: data
            }];

            let dialog = new frappe.ui.Dialog({
                title: 'Stock Balance',
                size: 'extra-large',
                fields: fields
            });

            dialog.show();
        });
    }
});
2 Likes