Rename Attached File in Sales Order Item Child Table Based on Sales Order ID + Customer Name

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:

  1. In the design_file field

  2. In File.file_name

  3. 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:

  1. Identify the parent Sales Order.

  2. Fetch the Sales Order ID and Customer.

  3. Preserve the original file extension.

  4. 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:

  • The design_file Attach field

  • File.file_name

  • File.file_url

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.