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

visualforce controller extensions question

what is the use case for having multiple extensions for a single visualforced page. where do we actually use it??


Can't we have entire logic in the same controller extension?



The use case is if you have a complex application and want to organize your Apex controller code into multiple classes.


You can definitely put all of your Apex logic into a single class and use that as a controller extension, that's a style and standards preference. For many apps it works just fine.


But when you have a lot of different concerns, it's nice to be able to separate them into distinct classes, for organization and sanity saving.


I love the verb "visualforced", I am not sure why, I just do :-)


The answer to your question is definately "yes". It may not be ideal though in all situations.


Multiple extensions can come into play when you have more than one visualforce page for the same object each requiring some specialist code to support it. In this scenario you could:


  1. Write a single extension to cover all pages (with a lot of "if" statements).
  2. Write a single extension for each page (with duplicated code).
  3. Write a single extension for each page (with the common code farmed into a single 'utility' class).
  4. Write a base extension for all pages and extra extensions that add or change functionality depending on what the page requires.

Each way above can be made to work and each has certain advantages (and disadvantages). To be honest it is a matter of style and personal/team preference. Personally I wouldn't do 2 and tend to aviod 1, but that is just me.


There is always change-over-time to consider as well. I have definately started with having a single extension on a single page and then as time/requirements change the number of pages needed, I then look at how I plan to tackle things not only to meet the changes but also plan for the unknown future ones. 


Thanks for the quick reply.

I have coded a page with two extensions. I got some refresh issues while testing the page. 


As either extensions does not have access to sobjects queried in other extensions, I have to rewrite/copy the code from second  extension to the first extension to solve the refresh issues.


I am curious if there is way to access the member variables defined in second extension while coding the in first extension directly. 



Hmm good question, unfortunately I don't know, never tried. Generally if I have multiple extensions it is to either impliment autonomous code or occassionaly to overwrite the way a particular method works (in which yes I am rewriting some code).


May be someone else does things this way and can answer.


By the description of the problem in your situation, I think I would taking Option 3 from my list.