Stat Tracker

Thursday, December 9, 2010

Salesforce Winter 10 - Attachment Trigger Bug (before Insert, after Insert)

This past week I discovered a very annoying bug with Attachment Triggers in Salesforce. In Salesforce Winter 10 release we can now assign triggers to the Attachment object in Salesforce. Note that you can't do this via the browser setup, you'll need to be inside the IDE.  The problem is that the triggers for insert won't always fire from the browser user interface!

For the project I'm working on I had to build a trigger for the Attachment object in Salesforce. I built the trigger, the trigger handler class, and the unit test class. Everything looked perfect. Unit tests were passing with 100% and all the system.assertEquals statements were showing the business logic was working for all the DML statements (insert, update, delete, etc). So I was feeling good about getting a decent amount of work done fairly rapidly, and thats when I hit the wall.

I attempted to do some spot checking on the Attachment functionality via the browser the way a standard user would attach a document. Then I notice that the fields have incorrect values, almost as if the trigger didn't fire! I open up the good old system log window, set the debug options, and tried again. I didn't see the Attachment trigger firing at all? What the heck? It worked in my unit tests during inserts, why didn't it fire? Its a DML / trigger, it shouldn't matter where you invoke it because at database level it will fire.

Fast forward to 3 days after opening a premier support ticket that had to be escalated three times to the R&D group at Salesforce. They inform me that its a bug in the system, and they are working to resolve it in the future.

So as a workaround I had to build out some Batch and Scheduled Apex to do the processing. The client was OK with this solution, as the data can be stale within a day. But very frustrating! Salesforce is usually pretty good at releases and bug fixes, so hopefully this will be fixed in the near future.

So my advice is to avoid writing Attachment triggers in Salesforce until April 10 release (hopefully).