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
vlrvlr 

Reuse Visualforce page and controller for a second site

I am new to Salesforce, so excuse me if I missed he obvious.

We have a Saleforce Site setup which is called ContactDirectory. I received a requirement to split this page to two pages: one for faculty, and another for staff. I tried two approaches trying to avoid the third approach.

My first thought was that I can simply choose Faculty Record Type for Contact object in a profile of the first site, and Staff Record Type for the second site. It turned out I misunderstand the purpose of selecting record types in profile of the site. I am still not clear on it, but it did not work.

My second way was to retrieve URL in a controller and then alternate SOQL query conditioned to this URL. URL which contains faculty will only select records for Faculty Record Type etc. Here I am not sure if it is good idea to build logic around URL. The main reason I am stack with idea is I do not know how to fix test cases that use PageReference to trigger controller. Specifically, I do not know how to set/pass URL when triggering VF page.

The third option would be just to copy everything for the second Site.

What would the best approach here? Should I try to reuse VF page and controller for similar sites?

Thanks.
Best Answer chosen by Admin (Salesforce Developers) 
GSBassoGSBasso

So your thinking is to use the URL to determine which record type to query for, is that the idea?

 

That's not necessarily bad, but I'd probably be inclined to have separate controllers for each page (each controller would know which record type to query for) and then create a helper class that both controllers would make use of (the bulk of the work would be done in the helper class).

 

You could also explore having the record type configured through a hierarchical custom setting based on the user and/or profile. This approach would only make sense if the set of users/profiles accessing each page was distinct.

All Answers

GSBassoGSBasso

In terms of testing, you'd need to do something like the following in your test method(s) before instantiating your controller instance:

 

PageReference pr = Page.MyPage;
pr.getParameters().put('param1', 'value1');
Test.setCurrentPage(pr);

MyPageController ctrl = new MyPageController(...)

 

vlrvlr

So, I have this code in the controller:

 

        // another opttion
        //ApexPages.currentPage().getHeaders().get('referer');
        String url =System.URL.getCurrentRequestUrl().toExternalForm();

 and I have this code for testing this controller:

 

PageReference ref = new PageReference('/apex/HtmlContactDirectory');
        ref.getParameters().put('rType', 'Faculty');
        Test.setCurrentPage(ref);
        ContactController controller = new ContactController();
        System.assertEquals(numberOfRecords, controller.contacts.size());        

 How can I use parameters to set the URL? Or, do you suggest to retrieve it in VF page, store to parameter and use the parameter in the controller ? Is it good idea to build logic around URL, instead of just copying VF page and Controller?

 

Thanks.

GSBassoGSBasso

Based on your test I don't undersand why you simply aren't retrieving the record type value in your controller as follows:

 

String rType = ApexPages.currentPage().getParameters().get('rType');

 

vlrvlr

Because it is a Force.com Site with public access, and this scenario is also possible:

 

if(rTypeName == null || rTypeName == '')

GSBassoGSBasso

So your thinking is to use the URL to determine which record type to query for, is that the idea?

 

That's not necessarily bad, but I'd probably be inclined to have separate controllers for each page (each controller would know which record type to query for) and then create a helper class that both controllers would make use of (the bulk of the work would be done in the helper class).

 

You could also explore having the record type configured through a hierarchical custom setting based on the user and/or profile. This approach would only make sense if the set of users/profiles accessing each page was distinct.

This was selected as the best answer
vlrvlr
So, there is no way to set/replace URL to PageReference during testing, right ?
GSBassoGSBasso

Yes, as you were doing it in one of your earlier posts:

 

PageReference pr = new PageReference('http://somewhere.com');
Test.setCurrentPage(pr);

 

I've run into issues with trying to change the page later on in the same test method so I typically create separate test methods for each combination of pages/parameters.

 

This might seem like a bother but if each method is doing more-or-less the same thing I'll parameterize this in a helper method that each test method invokes.

 

Hopefully this made some sense.