function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

Purpose of AddFileds Method

Hi follks,
        Can anyone tell me what is the purpose of AddFields Method in Standard Controller?
I Cant get what they provided in salesforce doc.
I need a clear explanation with example and please explain me when should I use it?
Any help would be greatly appreciate

Thanks in advance
Karthick
Best Answer chosen by SS Karthick
SarfarajSarfaraj
@Karthick

Check this reference
https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_addFields.htm

By definition: "When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup".

However this has some limitation. It depends on how are you using the markups in your VF page. If for some complex scenario the standard controller can not determine which all fields you are going to use in your VF, you may get this error,
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field:

Here comes the utility of addFields method. You can explicitly tell the standard controller that, add this fields to the query. It comes with some restriction: Should be called before the record is loaded (by using the controller.getRecord method) or controller instance should be reset before calling it. Refer to the link above for further details.

Regarding the test class, you can not test it. And I think probably you don't need it to test. Just bypass the call to addFields for test codes using a condition like this (in your controller),
if(!Test.isRunningTest())
    controller.addFields(<list instance>);
This is an open bug of addFields method. Please vote for this idea,
https://success.salesforce.com/ideaView?id=08730000000YmzmAAC

--Akram

All Answers

BalajiRanganathanBalajiRanganathan
AddFields method is useful when you use dynamic bindings in visualforce.

if you have some expression is visual force ie {!method}, as per the visualforce flow salesforce calls the getter method for the expression after calling the constructor. 
For dynamic bindings you can write generic visualforce pages such that the getter methods need to be called are determined dynamically at runtime.

https://www.salesforce.com/us/developer/docs/pages/Content/pages_dynamic_vf.htm

so when you use dynamic binding you can call the addFields method in the constructor of the controller so that all the getter methods will be called
those are used in the VF page dynamic expressions. so this method is not useful if you are not using dynamic bindings
SS KarthickSS Karthick
@BalajiRanganathan
How to test the addfield methods ?

Thanks in advance
Karthick
BalajiRanganathanBalajiRanganathan
do u mean how to write test code for the addField method?

Since mostly you will be calling this method with the controller constructor, when you create the construction object in the test code this method 
will also be called.
SS KarthickSS Karthick
@BalajiRanganathan,
          I cant call this method in test class
I got the below exception
System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller.
BalajiRanganathanBalajiRanganathan
https://developer.salesforce.com/forums/ForumsMain?id=906F000000090TrIAI
SarfarajSarfaraj
@Karthick

Check this reference
https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_addFields.htm

By definition: "When a Visualforce page is loaded, the fields accessible to the page are based on the fields referenced in the Visualforce markup".

However this has some limitation. It depends on how are you using the markups in your VF page. If for some complex scenario the standard controller can not determine which all fields you are going to use in your VF, you may get this error,
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field:

Here comes the utility of addFields method. You can explicitly tell the standard controller that, add this fields to the query. It comes with some restriction: Should be called before the record is loaded (by using the controller.getRecord method) or controller instance should be reset before calling it. Refer to the link above for further details.

Regarding the test class, you can not test it. And I think probably you don't need it to test. Just bypass the call to addFields for test codes using a condition like this (in your controller),
if(!Test.isRunningTest())
    controller.addFields(<list instance>);
This is an open bug of addFields method. Please vote for this idea,
https://success.salesforce.com/ideaView?id=08730000000YmzmAAC

--Akram
This was selected as the best answer
SS KarthickSS Karthick
Many Thanks Akram