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
Molson94Molson94 

VisualForce: Validating URL Parameters When Page Loads

Hi All,

I am looking to create a series of pages that allow for creation of multiple custom objects. The first step however before anything can be rendered or created is to check the 'v' parameter in the URL to ensure that the vendor exists.

i keep running into problems when i look to inslude the logic in the controller constructor. It is my understanding from the documentation that the controller constructor will be processed first, then getter, then actions etc...

The below logic either will not compile, because the contrstor cannot return a value, and thus not redirect the page. Or calling the PageReference method booter() does not do anything.

Below is the controller code i have currently:

public class ccController {
    public List<Vendor__c> vendor;

 //Redirects
    PageReference booted = new PageReference ('http://www.google.com');
    
    
    //controller constructor
    public ccController() {
        vendor = [SELECT id, name FROM Vendor__c WHERE id = :ApexPages.currentPage().getParameters().get('v') LIMIT 1];
        if (vendor.size() == 1){
            //vendor is valid do some stuff
            
        }else{
            //vendor is not valid, redirect to another page
            //booted.setRedirect(true);
            //return booted;
            booter();
        }
  
    public PageReference booter() {
        booted.SetRedirect(true);
        return booted;
        }
       
    public PageReference save() {
           return null;
    }
    
    public PageReference nextStep(){
        return null;
    }
    
}

Many thanks in advance for any feedback and nudges in the right direction.
Best Answer chosen by Molson94
Jim JamJim Jam
Instead of having your logic in the Controller constructor method, move it to its own public method and call that method from the visualforce <Apex:page .. action attribute. Ie. the very first line in your Visualforce page. Salesforce advises that this attribute should be used for page redirections not for initialisations so is probably good for you in this instance.

reference: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm

All Answers

Jim JamJim Jam
Instead of having your logic in the Controller constructor method, move it to its own public method and call that method from the visualforce <Apex:page .. action attribute. Ie. the very first line in your Visualforce page. Salesforce advises that this attribute should be used for page redirections not for initialisations so is probably good for you in this instance.

reference: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm
This was selected as the best answer
Molson94Molson94
Thanks for the guidance Frank.

I actually went a slightly different route and put the logic in the "getters" and had the page render dynamically based on those results. Rather than redirect the user to a completely separate page.

But, your response will help out on another project where i need to force a redirect.

Thanks again!