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
boyd_rboyd_r 

URL Navigation for custom objects

I was looking for a definative answer on how to build navigation to a custom object in Apex and Visualforce. There are 3 answers depending on the scenerio.

 

The first solution was based on using the URLs of standard objects as defined in this blog Thoughts on Salesforce.

 

So your code would look something like this.

 

 

PageReference pageRef = New PageReference(/001/o);
pageRef.setRedirect(true);
return pageRef;

 

 

This brings you to the Accounts page. Thats fine for standard objects and assumes salesforce doesn't change these codes in the future. Assumption being the mother of all.... etc

 

If the url is to another visualforce page that's straight forward

 

 

PageReference pageRef = New PageReference(/apex/MyPage);
pageRef.setRedirect(true);
return pageRef;

 

 

Use <apex:param....> to pass parameters in the URL

 

 

But what if you've created a custom object and just want to navigate to the custom tab salesforce provides from a Visualforce page. You can use the first method a find the url used for the tab. HOWEVER there is no guarantee this will be the url if you distribute the app or move orgs. Its not a visualforce page so what do you?

 

The trick is to use the Schema class in you controller and find its Key Prefix.

 

 

//Get a reference to the Custom Object and gets its Key
Schema.DescribeSObjectResult result = customObject__c.sObjectType.getDescribe();

//navigate to the View Page
PageReference pageRef = New PageReference('/' + result.getKeyPrefix() + '/o');
pageRef.setRedirect(true);
return pageRef; 

The /o instructs the redirect to go to the View page of the custom tab. Using /e creates a new record. For more information go to this blog Salesforce URLS

 

 

Hope this helps

 

Pradeep_NavatarPradeep_Navatar

You can use this Page reference through javascript also :

<a  href="/{!prefixOrc}/e?retURL={!$CurrentPage.parameters.id}" target="_parent">

where prefixOrc we can get from Apex .

In apex we can write this:


Schema.DescribeSObjectResult sa = (ObjectName).sObjectType.getDescribe();

     
prefixOrc = sa.getKeyPrefix();

*ObjectName = It is the Api Name of the Object Where You want to take the pagerefernce

 

Hope this helps.