I'd suggest to try to run the script on update, or to configure a content handler that would allow to do this. See example of this in the records management module - behaviour-context.xml configures several javascript content handlers that perform some actions. I believe the configuration and file related to onCreateChildAssociation_rmaFilePlan_cmContains.js does similar stuff to what you need - it allows the user to upload the file, and then pre-pends the filename with some IDs.