I have the requirement to create a validation rule that only allow you to set the status in closed won if a file is attached to the opportunity. To achieve this, I have created a custom field which is a counter and a custom trigger on ContentDocumentLink object which update that counter. I have tested it in my sandbox and works great but when I go in production and I try to add the file to the opportunity I get the error: Can't add 1 file to Opportunity.
I found this article: https://help.salesforce.com/s/articleView?id=000381870&type=1 where it says that the error is caused because of the custom trigger, and that is true because if I disable the trigger I can add files again.
I don't understand why in the sandbox works well and not in production, I don't know what I am missing. Any idea?
Due we can't create flows on CDL object and my trigger doesn't want to work, any other idea to achieve my requirement?
Thanks in advance