[SOLVED] How do you assign a value to table multi-select in the client script?

I’ve been fiddling with Table Multiselect (TMS) and cant get it to save even when the values show up in the browser. Maybe someone here will help.

My goal is to assign values (crops) to a TMS based on another field (Spray program). But I get errors when saving:


I’ve included my code, my form and console in this screenshot. Here’s my snipped of the assignment:

 spray_program(frm){
    if(frm.doc.spray_program){

        frappe.call({
            method: "frappe.client.get",
            args: {
                doctype: "Spray Program",
                name: frm.doc.spray_program,
                async: true
            },
            callback: (rs) => {
                let res = rs.message.sp_crops;
                let crops = ""
                let crop_list = []

                for (var index in res) {            
                    crops = crops + res[index].crop + ", "; //using a text field
                    //using a table multi-select field
                    crop_list.push({ name: res[index].name, crop: res[index].crop, doctype: res[index].doctype });
                }
                console.log(crop_list);
                
                frm.doc.crops = crops; //temp use text // iterate
                frm.refresh_field('crops');
                frm.doc.user_crops = crop_list; //temp use text // iterate
                frm.refresh_field('user_crops');

                /**
                 Failed
                 frm.doc.user_crops = [{ name: , crop: "F116-124", doctype: "Crops" }] //added dt
                 Needs actual doctype

                 0: {name: 'CROP-001', owner: 'adam@jjjj.com', creation: '2022-09-05 19:07:59.376017', modified: '2022-09-05 19:12:29.548265', modified_by: 'adam@jjjjj.com', …}

                 0: 
                    creation
                    : 
                    "2022-09-05 19:07:59.376017"
                    crop: "F116-124"
                    docstatus
                    : 
                    0
                    doctype
                    : 
                    "Crops"
                    idx
                    : 
                    1
                    modified
                    : 
                    "2022-09-05 19:12:29.548265"
                    modified_by
                    : 
                    "adam@jjjjj.com"
                    name
                    : 
                    "CROP-001"
                    owner
                    : 
                    "adam@jjjjj.com"
                    parent
                    : 
                    "001-1234-aug"
                    parentfield
                    : 
                    "sp_crops"
                    parenttype
                    : 
                    "Spray Program"
                 */
                
            }
        });
    }
},

Assignment with frm.set_value solved it :triumph:

 spray_program(frm){
    if(frm.doc.spray_program){

        frappe.call({
            method: "frappe.client.get",
            args: {
                doctype: "Spray Program",
                name: frm.doc.spray_program,
                async: true
            },
            callback: (rs) => {
                let res = rs.message.sp_crops;
                let crops = []

                for (var index in res) {         
                    crops.push(res[index]);
                }

                frm.set_value("user_crops", crops);
                refresh_field('user_crops');
            }
        });
    }
},

Is there a python method to do it??

found any?

Simply You have to go to Child Table Which You have Given In Options for Tab MultiSelect. Click on Field and Enable Properties In List View, In List Filter and In Filter as shown in image below.
image

and for table multiselect field enable properties
Allow in Quick Entry, In List Filter and In Filter as shown in image below.
image

This Will Solve the Issue. without any script.

To copy values from field use :

frappe.call({
        method: "frappe.client.get",
        args: {
            doctype: "Spray Program",
            name: frm.doc.spray_program,
            async: true
        },
        callback: (rs) => {
            let res = rs.message.sp_crops;
            let crops = []

            for (var index in res) {         
                crops.push(res[index]);
            }

            frm.set_value("user_crops", crops);
            refresh_field('user_crops');
        }
    });
1 Like

Will test this and mark your answer as the solution if it works for me as well. Thank you!