You can remove the trigger and simply insert the content version with the id of the record you want to associate it to in the FirstPublishLocationId and Salesforce will setup the content document relationship for you.
Read more here
https://developer.salesforce.com/docs/atlas.en-us.234.0.object_reference.meta/object_reference/sforce_api_objects_contentversion.htm
You can remove the trigger and simply insert the content version with the id of the record you want to associate it to in the FirstPublishLocationId and Salesforce will setup the content document relationship for you.
Read more here
https://developer.salesforce.com/docs/atlas.en-us.234.0.object_reference.meta/object_reference/sforce_api_objects_contentversion.htm