How to copy an attachment from a doctype to another doctype

Hi,
I wonder if it’s possible to copy an attachment inside a doctype to another doctype using python or js.
I created a Doctype where I attached ‘something.png’. When I submit the document I create an Expense Claim, but I didn’t find a way to attach ‘something.png’ to the newly created Expense Claim.

Is there a way to do that?

1 Like

Hi,
I think it’s possible to select an attachment from the uploaded files also visible under File Manager or maybe you are trying to achieve something entirely different?

Hi @mwai,
what I want to do is copy the attachments from Doctype1 and paste it in Doctype2 using python or js.

Here are more details:
In a custom Doctype, I created a ‘Simplified Expense Claim’ (so the user can enter the amount, date, reason…) and attach the image of the invoice.
Once this document is submitted, I take the given information and create a real Expense Claim. The whole process is working fine, except for the attachments. I would like to take the image from ‘Simplified Expense Claim’ and add it inside ‘Expense Claim’ when I create it. The process should be done by code only.

Any Ideas?

@mel_erp I have a similar requirement, did you ever find a solution?

Thanks.

This is what I to do to add attached PDF file on order on validate

The file CGV.pdf was previously uploaded in Frappe File module in home/, but I think you can get required data by get_doc your specifics DocType

hook.py in custom app


doc_events = {
    "Sales Order": {
"on_submit": "customapp.customapp.custom_scripts_py.sales_order.add_cgv_as_attachement"
    },
}

in customapp.customapp.custom_scripts_py.sales_order.py


def add_cgv_as_attachement(doc, method):
    add_cgv = 1
    for file_cgv in frappe.get_all("File", fields=["name", "file_name", "file_url", "is_private"],
                                   filters={"attached_to_name": doc.name, "attached_to_doctype": 'Sales Order'}):
        if file_cgv.file_name == 'CGV.PDF':
            add_cgv = 0

    if add_cgv == 1:
        ret = frappe.get_doc({
            "doctype": "File",
            "attached_to_doctype": 'Sales Order',
            "attached_to_name": doc.name,
            "attached_to_field": None,
            "folder": 'Home/Attachments',
            "file_name": None,
            "file_url": '/files/CGV.PDF',
            "is_private": 0,
            "content": None
        })
        if ret is not None:
            ret.save(0)
            frappe.msgprint("File XXXXX.pdf added")
1 Like

Hi, Just trying to understand the use case here.
Why would you want a new doctype when the original claim document exists?
Just trying to understand the requirement and use