I recently came upon a bug when attempting to implement the new addFields method on an Apex Standard Controller class. The purpose of the add fields method is to allow a developer to specify fields that the standard controller should query along with the fields that are defined in the markup of a Visualforce page. This is very useful when you need to evaluate some fields but don't need to display them.

Prior to this feature you had a couple of options to get the additonal fields you needed:

  • Add the fields to your Visualforce page as hidden input fields
  • Perform a query in the constructor to get the fields you need

Both of the solutions identified above are not ideal. The first option requires you to muddy up your markup with additional fields and creates a bit of a security issue if the data should not be seen by users or anyone for that matter who can right click on the page and click "view source".

The second option is much more secure but is inefficient as it requires a query call just to get a couple of extra fields. However, in all fairness, in the context of performance it's not really an issue as the query is pretty fast. But just knowing that my code is performing an extra query when the information could/should be readily available in the StandardController was one of those little things that bugged me but I didn't have the time to really care about.

Thankfully, the Salesforce developers added a feature to solve for this scenario by adding a method called addFields to the StandardController class that allows the developer to supply a list of fields that the controller should query for in addition to the fields defined in the Visualforce markup.

Seeing the benefits this feature offers I started implementing it and everything worked great! I added the fields to the controller and wrote my controller logic to consume the fields, tested it a few times through the interface and was happy with the performance and how clean my markup and code was. But then everything kind of came to a halt when I began to write my unit tests.

When I ran my test I got the following error message:

You cannot call addFields after you've already loaded the data. This must be the first thing in your constructor

The error message above usually occurs if you attempt to use the add fields method more than once but no where in my code was I calling this method more than once. After some trial and error to see if I had done something wrong I contacted Salesforce support and they were able to replicate the results for my particular instance and have leaned towards it being a bug but at this point I do not know if it is a known issue with a patch on the way or something that is just coming up.

The current status of this issue is that it is being researched as a possible bug. In the meantime, I have worked around the issue by querying for the fields I needed so that my code and unit tests can successfully run.

For now the wait is on and I will post an update once I get a response back from Salesforce support.