+ Start a Discussion
Ben Merton 15Ben Merton 15 

Calling a controller into a VisualForce page URL

I am using this code to return the first three letters of a Custom Object's id, so that I can then bypass the standard object landing page and go straight to a list view of that Custom Object:
 
//This class is used as a controller for allowing the Corporate Statements tab to land directly on the List View

public class PageRef{
public PageReference exit(){
//change the Any_ObjectName__c with your Custom or Standard Object name.
Schema.DescribeSObjectResult anySObjectSchema = Corporate_Statements__c.SObjectType.getDescribe();
String objectIdPrefix = anySObjectSchema.getKeyPrefix();
PageReference pageReference = new PageReference('/'+objectIdPrefix+'/o');
pageReference.setRedirect(true);
return pageReference;
}
}

I am not clear exactly how I am meant to now call this into a VisualForce page:
 
<apex:page controller="PageRef">
action = "{!URLFOR(pageReference)};
</apex:page>
Any help?
 
Best Answer chosen by Ben Merton 15
Nitin Paliwal 9Nitin Paliwal 9
Hey,
Your controller code is fine.
On the visualforce page add the "action" property in "apex:page tag" like,

<apex:page controller="PageRef" action = "{!exit}">
</apex:page>

This should solve your issue.

Thanks
Nitin

All Answers

Siddharth ManiSiddharth Mani
Not sure if this is what you are looking for, but try this:

Change your method name as getExit as below and use the below page as reference:
public PageReference getExit(){
...........rest of the code
Page:
<apex:page controller="PageRef">
<apex:outputLink value="{!Exit}">Click Here!!</apex:outputLink>
</apex:page>

 
Nitin Paliwal 9Nitin Paliwal 9
Hey,
Your controller code is fine.
On the visualforce page add the "action" property in "apex:page tag" like,

<apex:page controller="PageRef" action = "{!exit}">
</apex:page>

This should solve your issue.

Thanks
Nitin
This was selected as the best answer
Ben Merton 15Ben Merton 15
Nitin

Thanks, but now the page isn't loading at all.  It seems to be looping on this URLhttps://c.na22.visual.force.com/apex/CorporateStatementsListView?save_new=1&sfdc.override=1

And switching between 'waiting for c.na22.visual.force.com and na22.visual.force.com??

Ben
Nitin Paliwal 9Nitin Paliwal 9
Hey Ben,
Have you overriden the view page or something, could you tell me what exactly do you want to do?

Thanks
Nitin
Ben Merton 15Ben Merton 15
Nitin

I am trying to override the standard landing page of a custom tab.  ie, I want to be able to click on a tab and have it point directly to the List View for that tab without having to go to the 'Recent Items' page.  I have updated the Custom Tabs Button to redirect to the VisualForce page, but it isn't open (ie yes the Custom Tab functionality is overridden - it is now pointing to the VF page that you have referenced above, but it isn't loading)

Ben
Nitin Paliwal 9Nitin Paliwal 9
I tried the same thing, create a vf page and controller, created a visualforce tab and linked the vf page with this tab. When i am clicking on the tab, it is perfectly redirecting me to the list view.

The profile with which you are checking has access  to that page right?
 
Ben Merton 15Ben Merton 15
Yes I am logged in as an admin.  It's flicking between c.na22.visual.force.com and .na22.visual.force.com.  Interestingly, the same thing is happening when I click 'Preview' in the VF page.  I have deleted browser cookies etc and the same thing is still happening.  Am I going mad?
Ben Merton 15Ben Merton 15
I just tried in IE, and it is repeatedly clicking when it flicks between the two URLs, meaning that something must be looping?

Just to confirm the code is correct:

VF:
<apex:page controller="PageRef" action="{!exit}"> </apex:page>


Controller:
public class PageRef{
public PageReference exit(){
//change the Any_ObjectName__c with your Custom or Standard Object name.
Schema.DescribeSObjectResult anySObjectSchema = Corporate_Statements__c.SObjectType.getDescribe();
String objectIdPrefix = anySObjectSchema.getKeyPrefix();
PageReference pageReference = new PageReference('/'+objectIdPrefix+'/o');
pageReference.setRedirect(true);
return pageReference;
}
}




 
Ben Merton 15Ben Merton 15
I found the answer from another post.  the problem was with line 6, which needed to have the /0 string removed:
 
PageReference pageReference = new PageReference('/'+objectIdPrefix);