*****UPDATE June 30, 2011******
Shortly after this post I was contacted by Salesforce’s Apex product manager who wanted to follow up on this issue and help resolve it. Major kudos to Salesforce for monitoring the blogosphere and addressing the problems of their partners and customers. After spending some time trying to recreate this problem the SFDC team could not replicate it, and so apparently whatever was causing this quirky issue to happen has been resolved, effectively making the rest of this post obsolete. But, you can read on for the history if you’d like… 😉
The good news about working on the Salesforce platform is that three times a year, while you’re lying asleep at night, the little Salesforce elves are hard at work rolling out the newest release. The bad news is that sometimes on a Monday morning you start seeing Apex errors in your inbox. This past release of the Summer ’11 upgrade was one of those times for me. I thought I’d share the details to prevent anyone else from banging their noggin’ against the wall. Spoiler Alert! The story ends happily with a working solution.
We had a portion of Apex code that, when executed, created a custom object which was the detail side of a master-detail relationship. Nothing special going on here, just straightforwardness.
On the Monday morning after the org was upgraded to Summer ’11 errors started to kick off whenever this particular piece of code was executed. The error given was “Field Not Writable” and it referred to the lookup field that looked up to the Master object. For all the visual learners out there (like me!) see the diagram below.
Whenever an Object B was attempting to be inserted we would get an error on Field 1 saying “Field Not Writable”. Very strange since two days earlier this was working splendidly and nothing had changed since. After quite some time of trying to track down the root problem we submitted a Case to Salesforce assuming this was some bug related to the upgrade. As it usually goes, shortly after submitting the case we developed a solution that worked. Here it is…
This is actually so simple it hurts. The object was being created like this.
Object_B__c objB = new Object_B__c(); objB.field1__c = objectAId; insert objB;
The solution was simply to instantiate the Master object field in the constructor. Like so…
Object_B__c objB = new Object_B__c(field1__c = objectAId); insert objB;
A very subtle quirk in the new release but nonetheless one that can cost you hours of time trying to understand and resolve. Salesforce support did respond and let me know that they were unaware this change had happened. The case was forwarded on for analysis. No word yet on whether this was intended or unintended but for now all is good.
I hope this helps and as always I look forward to any comments or feedback.