+ Start a Discussion

Redirect user to another VF page if no record id was set in URL



I am currently trying implement a visualforce page with a standard controller and an extenssion. If the user puts in the url ".../apex/mySite?id=0000001", then that is great. But if the user enters ".../apex/mySite" with no record id in the URL, then the page crashes... as expected. How do I redirect the user to say record number 000001 if they don't enter a record if in the URL?


I've seen <apex:page action="..."> and it said this action should be used to redirect the user to another page, but I don't know how to do that.


Thanks in advanced!

Suresh RaghuramSuresh Raghuram

In the mysite page display a message to enter the record id if they want go to the record page

Is there a way I can just automatically redirect them to record id '000001' using apex code?


Use action method of page vf tag like this :


<apex:page controller="pageController" action="{!redirectTo000001}">



public class pageController
	public pageController()
	public pagereference redirectTo000001()
		String idFromURL = ApexPages.currentPage().getParameters().get('id');
		if(idFromURL == null || idFromURL == '')
			pagereference pg = new pagereference('/apex/mySite?id='+idFromURL);
			return pg;
		else return null;



This worked in a way but now I have another problem. I used a custom object's record as the id by using the following code snippets:



pinnedContent = [SELECT id FROM KN_Content__c WHERE Pinned__c = true LIMIT 1];

PageReference pg = new PageReference('/apex/KnowledgeNetwork?id=' + pinnedContent.id);


However, the page redirects to the URL: apex/MySite?id={!pinnedContent}


It's not supposed to say ?id={!pinnedContent}, it's supposed to say ?id=000001, where 000001 is the pinnedContent.id.