How to fetch Multiple Attachments via client script

I want to fetch multiple Attachments via client script, all Attachments are images
and i want fetch in another doctype

image

Hi @nilpatel42,

Please check the @TurkerTunali post. Try it according to the scenario.

I hope this helps.

Thank You!

1 Like

Hii @NCP

i tried but its not working, here is script

frappe.ui.form.on('Doctype', {
    onload: function(frm) {
        fetchItemAttachments(frm);
    }
});

function fetchItemAttachments(frm) {
    let arrItemCode = [];

    $.each(frm.doc.items, function(i, d) {
        arrItemCode.push(d.model);
    });

    frappe.db.get_list("File", {
        filters: [
            ['attached_to_name', 'in', arrItemCode],
            ['attached_to_doctype', '=', "Item"]
        ],
        fields: ["name"]
    }).then((data) => {
        let attachments = [];
        if (data.length > 0) {

            data.forEach((attachment) => {
                attachments.push(attachment.name);
            });

            frm.set_value('image', attachments.join(',')); 
            frm.refresh_field('image'); 
        } else {
           
            frm.set_value('image', '');
            frm.refresh_field('image'); 
        }
    });
}

Please set your doctype name in script.

my doctype is custom created single doctype name is
“Price”

i tried with name but not working

Hii @NCP

this script is working for other doctype but in Which i want to add, its not adding button

in other single doctype its working well

function GetFileAttachments(frm, arrItemCode) { 
    // Get files which are added to given items
    frappe.db.get_list("File", {
        filters: [
            ['attached_to_name', 'in', arrItemCode],
            ['attached_to_doctype', '=', "Item"]
        ],
        fields: ["file_name", "file_url", "folder"],
        limit: 9999
    }).then((data) => {
        let arrFileExt = []; // File extensions
        let arrFileExtInfo = []; // JSON
        let strFileExt = "";
        let jsonFileInfo = {
            file_ext: [],
            file_count: []
        };
        let dIndex = -1; // File index search
        for (let i = 0; i < data.length; i++) {
            strFileExt = data[i].file_name.substring(data[i].file_name.lastIndexOf(".") + 1);
            if (arrFileExt.indexOf(strFileExt) === -1) {
                arrFileExt.push(strFileExt);
                arrFileExtInfo.push({ 'file_ext': strFileExt });
                jsonFileInfo.file_ext.push(strFileExt);
                jsonFileInfo.file_count.push(1);
            } else {
                dIndex = jsonFileInfo.file_ext.indexOf(strFileExt);
                jsonFileInfo.file_count[dIndex] = jsonFileInfo.file_count[dIndex] + 1;
            }
        }
        for (let i = 0; i < arrFileExtInfo.length; i++) {
            strFileExt = arrFileExtInfo[i].file_ext;
            dIndex = jsonFileInfo.file_ext.indexOf(strFileExt);
            arrFileExtInfo[i].file_count = jsonFileInfo.file_count[dIndex];
        }

        const fields = [
            {
                label: 'Files',
                fieldtype: 'Table',
                fieldname: 'files',
                description: __('Select File Types'),
                fields: [
                    {
                        fieldtype: 'Data',
                        fieldname: 'file_ext',
                        options: '',
                        reqd: 0,
                        readonly: 1,
                        label: __('File Type'),
                        in_list_view: 1
                    },
                    {
                        fieldtype: 'Int',
                        fieldname: 'file_count',
                        options: '',
                        reqd: 0,
                        readonly: 1,
                        label: __('File Count'),
                        in_list_view: 1
                    }
                ],
                data: arrFileExtInfo,
                get_data: () => {
                    return r.message // ERR
                }
            }]
        var d = new frappe.ui.Dialog({
            title: __('Select File Types'),
            fields: fields,
            primary_action: function (dataTable) {
                let arrPromFile = [];
                for (let i = 0; i < d.fields_dict.files.df.data.length; i++) { // For each file type
                    strFileExt = d.fields_dict.files.df.data[i].file_ext;
                    // Is the file type selected?
                    if (typeof d.fields_dict.files.df.data[i].__checked !== 'undefined') {
                        if (d.fields_dict.files.df.data[i].__checked === 1) {
                            // Select the files which have the same extension
                            for (let j = 0; j < data.length; j++) { // For each file
                                if (strFileExt == data[j].file_name.substring(data[j].file_name.lastIndexOf(".") + 1)) {
                                    // Extensions match
                                    const docFile = frappe.model.get_new_doc('File');
                                    docFile.file_name = data[j].file_name;
                                    docFile.attached_to_doctype = frm.doctype;
                                    docFile.attached_to_name = frm.docname;
                                    docFile.file_url = data[j].file_url;
                                    docFile.folder = data[j].folder;
                                    arrPromFile.push(frappe.db.insert(docFile));
                                }
                            }
                        }
                    }
                } // File operations are completed.
                d.hide();
                Promise.all(arrPromFile).then((result) => {
                    frm.reload_doc();
                })
            },
            primary_action_label: __('Attach Files')
        });
        d.show();
    });
}

frappe.ui.form.on('Price', {
    refresh(frm) {
        frm.add_custom_button(__('Get Item Attachments'), function () {
            let arrItemCode = [];

            arrItemCode.push(frm.doc.model);

            GetFileAttachments(frm, arrItemCode);
        });
    }
});

Its working already i have one button script in that doctype, when i am adding new script below old script its not adding button,

when i add new script in beginning it working

thank you @NCP