Greetings All,I having a issue to link attachment documents from bom operation to workorder operation and also its goes to job cards since we are wood working industry we need to provide separate documents for each operation ex 1.pasting list for cold press station, 2.cutting list for panel cutting station,and goes on edgebanding, boring and more…the need is we need to configure each operation on bom including attachments that goes to workorder operation then jobcard please if anyone know how can i achieve this help me.
Someone Please help us on this one Please
Hi,
You can achieve the same using the server script to transfer the attachment from one Doctype to another Doctype. Below is the GPT generated code and not tested by me.
Please try and adapt as per your environment (GPT generated codes have some time incorrect fields and Doctype names):
To transfer file attachments from:
- BOM ➝ Work Order, and
- Work Order ➝ Job Card
…you can use ERPNext server scripts (either via hooks or workflow actions). Below is a server script for each step:
1. Server Script: Transfer BOM Attachments to Work Order
Trigger: On Work Order creation (After Insert
)
# Script Type: Server Script
# Doctype: Work Order
# Event: After Insert
import frappe
def copy_attachments(source_doctype, source_name, target_doctype, target_name):
attachments = frappe.get_all("File", filters={
"attached_to_doctype": source_doctype,
"attached_to_name": source_name,
"is_folder": 0
})
for file in attachments:
new_file = frappe.get_doc("File", file.name).as_dict()
new_file.update({
"attached_to_doctype": target_doctype,
"attached_to_name": target_name,
"name": None,
"modified": None,
"modified_by": None,
"creation": None,
"owner": frappe.session.user
})
frappe.get_doc(new_file).insert(ignore_permissions=True)
# From BOM to Work Order
bom = frappe.db.get_value("BOM", {"name": doc.bom_no}, "name")
if bom:
copy_attachments("BOM", bom, "Work Order", doc.name)
2. Server Script: Transfer Work Order Attachments to Job Card
Trigger: On Job Card creation (After Insert
)
# Script Type: Server Script
# Doctype: Job Card
# Event: After Insert
import frappe
def copy_attachments(source_doctype, source_name, target_doctype, target_name):
attachments = frappe.get_all("File", filters={
"attached_to_doctype": source_doctype,
"attached_to_name": source_name,
"is_folder": 0
})
for file in attachments:
new_file = frappe.get_doc("File", file.name).as_dict()
new_file.update({
"attached_to_doctype": target_doctype,
"attached_to_name": target_name,
"name": None,
"modified": None,
"modified_by": None,
"creation": None,
"owner": frappe.session.user
})
frappe.get_doc(new_file).insert(ignore_permissions=True)
# From Work Order to Job Card
if doc.work_order:
copy_attachments("Work Order", doc.work_order, "Job Card", doc.name)
Notes
- Make sure users running this script have permission to access
File
documents. - You can also batch this in a background job if many attachments are being transferred.
- If using custom scripts, add them from:
Settings > Customization > Server Script > New
Thanks,
Divyesh Mangroliya
@mangroliya,Thank You so much for your response sure will try it out and update you with screenshots.
I have managed to make it work its a long process tho i will update it properly once i write down the steps Thank you for the headsup @mangroliya .