How to redirect to the same VF page from the Constructor with parameters in Salesforce?

Sample Code:

Apex Class:

public with sharing class SampleClass{

    public SampleClass(){
    public PageReference doRedirect(){
        String param = ApexPages.currentPage().getParameters().get('UserId');
        if(param == null || param == ''){
            String userId = UserInfo.getUserId();
            PageReference pageRef = new PageReference('/apex/TestPage1');
            return pageRef;
            return null;

VF Page:

<apex:page controller="SampleClass" action="{!doRedirect}">
	This is your new Page

Leave a Reply