In many cases, after a custom action performed, we may need to redirect the page to a different page, could be another Visualforce page / standard Page like a list view of an object or any of default pages like view / edit page of a record or a new record entry page.
Page redirection happens in Apex code using PageReference class.
There are multiple ways to instantiate a page reference in Apex. Below are list of all possible ways.
Approach 1:
Approach 2:
Approach 3:
PS: Try not to hard code URLs and use above available options to build the URLs.
Page redirection happens in Apex code using PageReference class.
There are multiple ways to instantiate a page reference in Apex. Below are list of all possible ways.
Approach 1:
PageReference pageRef = new PageReference('***partialURL***');
PageReference pageRef = new PageReference('***FullURL***');
Eg:
PageReference pageRef = new PageReference('/apex/CustomPage1?id=xxxxxx');
PageReference pageRef = new PageReference('/a04');
PageReference pageRef = new PageReference('http://www.google.com');
Approach 2:
PageReference pageRef = Page.<<VF Page Name>>;
Eg:
PageReference pageRef = Page.CustomPage;
pageRef.getParameters().put('id','XXXXXXX');
Approach 3:
Refer stnadard pages of an Object / record
PageReference pageRef = new ApexPages.StandardController(recordObj).edit();
PageReference pageRef = new ApexPages.StandardController(recordObj).view();
Refer List view of an object
PageReference pageRef = new PageReference('/'+<<Object>>.sObjectType.getDescribe().getKeyPrefix());
PS: Try not to hard code URLs and use above available options to build the URLs.