I want to customize the attachment behavior inside the Sales Order Item child table.
Inside the Sales Order Items child table, I have a custom field called design_file (Attach field).
When a user uploads a file like:
-
abcd.png
-
sample.jpg
-
design.ai
I want ERPNext to automatically rename the uploaded file using this format:
<Sales Order ID>\_<Customer>.extension
Example:
-
Sales Order ID = Q038
-
Customer = xyz
Then uploaded file should become:
Q038_xyz.png
Expected Result
The renamed file should reflect everywhere:
-
In the design_file field
-
In File.file_name
-
In file_url
Example :
Q038_xyz.png
instead of original uploaded name.
This can be achieved by using a File DocType hook instead of handling the rename logic from the child table field directly.
The recommended approach is to add a before_insert hook on the File DocType. When a file is uploaded through the design_file Attach field in the Sales Order Item child table, the hook can:
-
Identify the parent Sales Order.
-
Fetch the Sales Order ID and Customer.
-
Preserve the original file extension.
-
Rename the file using the format:
<Sales Order ID>_<Customer>.<extension>
Example:
Original File: sample.jpg
Sales Order: Q038
Customer: XYZ
Renamed File: Q038_XYZ.jpg
By updating the File document before it is saved, the renamed file will be reflected consistently in:
This approach is preferable because it works regardless of how the file is uploaded (UI, API, custom scripts, etc.) and keeps the file naming logic centralized in one place.