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
ShikibuShikibu 

how to get url for a tab using apex (in the vf controller)?

I'm overriding "delete" on std object Asset, pointing it to a vf page whose controller should either delete the asset or (if it has related custom objects) redirect to a vf page which warns the user that these relationships will be broken, dispays the related list, and offers a "delete anyway" button.

 

Following delete of the asset, I want to emulate standard salesforce behavior, that is, display the Products tab home page.

 

How can I determine the url of the Products page within my apex controller?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ShikibuShikibu

Hi TLF,

 

The info you need is actually in my additional posts above, but a bit scattered. Here's some code that should work:

 

 

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Map<String,String> prefixKeyMap = new Map<String,String>{}; for(String sObj : gd.keySet()){ Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe(); prefixKeyMap.put(r.getName(), r.getKeyPrefix()); } String threeLetterCode = prefixKeyMap.get('Asset'); PageReference pageRef = new PageReference(threeLetterCode); system.debug( pageRef.getUrl() );

 

 

 

 

 

All Answers

TLFTLF
Hi Shikibu. Did you ever find a solution to this? I have to do something similar. I was just wondering if you solved this?
ShikibuShikibu

In the process of development, my code operates in a different way, and has no need to display that tab.

 

However, I think that you can just do new ApexPages and set partial-url to '/three-letter-code' for any object that has a tab (including all standard objects). The three letter code for Product2 is 01t. But I don't recommend you hard-code it. Instead, steal a small bit of the code shown below to dynamically determine this code.

 

I wanted a list of all three letter codes, and I extracted them with this combination of apex and some manipulation in my text editor:

 

 

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
Map<String,String> keyPrefixMap = new Map<String,String>{};
Set<String> keyPrefixSet = gd.keySet();
for(String sObj : keyPrefixSet){
Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe();
String tempName = r.getName();
String tempPrefix = r.getKeyPrefix();
System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']');
keyPrefixMap.put(tempPrefix,tempName);
}

for (String foo : keyPrefixMap.keySet()) {
system.debug( foo + ' ' + keyPrefixMap.get(foo));
}


// Run the above somewhere that you can snag the debug output
//(workbench, or system log, or exec anonymous in IDE or TextMate).

//
// then do the following steps in editor or shell
//
// replace .* ([a-zA-Z0-9]{3} [a-zA-Z0-9_]+)$ with $1
// sort

 

 Here are the codes for standard objects:

 

 

001 Account
002 Note
003 Contact
005 User
006 Opportunity
008 OpportunityHistory
00D Organization
00E UserRole
00G Group
00I Partner
00J OpportunityCompetitor
00K OpportunityContactRole
00P Attachment
00Q Lead
00T Task
00U Event
00X EmailTemplate
00a CaseComment
00b WebLink
00e Profile
00k OpportunityLineItem
00l Folder
00o OpportunityLineItemSchedule
00r AccountShare
00t OpportunityShare
00v CampaignMember
010 CaseSolution
011 GroupMember
012 RecordType
015 Document
016 BrandTemplate
018 EmailStatus
019 BusinessProcess
01D UserAccountTeamMember
01H MailmergeTemplate
01L CurrencyType
01M AccountTeamMember
01N Scontrol
01Y CampaignMemberStatus
01m BusinessHours
01n CaseShare
01o LeadShare
01p ApexClass
01q ApexTrigger
01s Pricebook2
01t Product2
01u PricebookEntry
020 EventAttendee
021 QuantityForecast
022 FiscalYearSettings
026 Period
027 RevenueForecast
028 OpportunityOverride
029 LineitemOverride
02Z AccountContactRole
02a ContractContactRole
02i Asset
02n CategoryNode
02o CategoryData
02s EmailMessage
035 SelfServiceUser
03g QueueSobject
03j CaseContactRole
03s ContactShare
03u UserPreference
04W RevenueForecastHistory
04X QuantityForecastHistory
04g ProcessInstance
04h ProcessInstanceStep
04i ProcessInstanceWorkitem
04m AdditionalNumber
04v CallCenter
04w DatedConversionRate
05X DocumentAttachmentMap
066 ApexPage
081 StaticResource
083 Vote
08s CampaignShare
091 EmailServicesFunction
093 EmailServicesAddress
099 ApexComponent
09a Community
0C0 Holiday
100 UserLicense
500 Case
501 Solution
608 ForecastShare
701 Campaign
707 AsyncApexJob
800 Contract
806 Approval
null AccountHistory

 

 

 

 

 

 

Message Edited by Shikibu on 04-28-2009 10:34 AM
Message Edited by Shikibu on 04-29-2009 07:35 AM
ShikibuShikibu
I just had cause to want to see a list of Assets, which don't have a tab. The technique shown above worked for me -- I entered salesforce.com/02i, and got the list I wanted.
TLFTLF

Ah, thanks. I knew that I could provide the three character id of the tab to render the tab, the part that I was unclear on was how to dynamically determine the id of a tab. I'll try your suggestion.

Karthi_VeluKarthi_Velu

Friend,

 

Did you got any solution for this issue, I am also having a requirement like, Dynamically get the "web tab url" on apex controller in order to redirect to another "web tab" page. without hardcode the web tab url, if any solution should be thanks full.

 

 

ShikibuShikibu

Hi TLF,

 

The info you need is actually in my additional posts above, but a bit scattered. Here's some code that should work:

 

 

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); Map<String,String> prefixKeyMap = new Map<String,String>{}; for(String sObj : gd.keySet()){ Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe(); prefixKeyMap.put(r.getName(), r.getKeyPrefix()); } String threeLetterCode = prefixKeyMap.get('Asset'); PageReference pageRef = new PageReference(threeLetterCode); system.debug( pageRef.getUrl() );

 

 

 

 

 

This was selected as the best answer
ShikibuShikibu

There is a much more straightforward solution, using URLFOR, here.

 

Salesforce: Please, please, please document the uses and arguments of URLFOR! We shouldn't have to hunt so much! 

dev_hedgedev_hedge

I would be reluctant to hardcode the 3 character keys even for standard objects.  I agree they probably will never change.  Rather than do a global describe for the whole org and throwing away 99% of the information returned I would suggest the following in your VF controller.

        String ObjectPrefix = {Insert Object Name Here}.sObjectType.getDescribe().getKeyPrefix();
              
        PageReference p = new PageReference('/'+ObjectPrefix);
        p.setRedirect(true);
  
               
        return p;  

 

This way you are only retrieving what you need and it works for custom objects....across your different orgs.

 

 

Cheers!

 

 

 

Adam PurkissAdam Purkiss

Thank you Shikibu!!

 

Even though you can get this value through a $URLFOR call, your utility method works great when you want to redirect as a result of an action method. For example, in the save() method in a custom controller that has a RichText field or an apex:inputFile in the VF page. In this scenario you can't get away with using oncomplete since this is not allowed (along with rerender) in combination with RichEdit or inputFile.

 

So now I can use the straightforward:

 

<apex:commandButton value="Save" action="{!save}"/>

 with my modified version of your code in my controller:

 

public PageReference save()
{
	PageReference retVal = getURL();
	
	...
	
	return retVal;
}


private PageReference getURL()
{
	// Get describe info and populate prefix map
	Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
	Map<String,String> prefixKeyMap = new Map<String,String>{};
	for(String sObj : gd.keySet())
	{
		Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
	   	prefixKeyMap.put(r.getName(), r.getKeyPrefix());
	}
		
	// Get prefex for MyCustomObject__c
	String threeLetterCode = prefixKeyMap.get('MyCustomObject__c');

	// Create pageRef from prefix
	PageReference pageRef = new PageReference('/' + threeLetterCode + '/o');
		
	// Return page MyCustomObject__c.Tab
	return pageRef;
} 

 

Thanks again!! Much appreciated.

 

Adam

Monalisa Das 22Monalisa Das 22
While returning the page reference you can use somerhing like 
return new PageReference('/'+Account.sObjectType.getDescribe().getKeyPrefix()+'/o');
Here Account can be replaced with the Object name of your choice