Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have VF page and placed that in home side bar component page and I am capturing the Salesforce instance URL from visual force using custom controller.

public String getReferer()
{ 
    BrowserURL = ApexPages.currentPage().getHeaders().get('referer'); 
    return browserURL;
} 

Now I want to fetch recorded id using Salesforce instance URL in VF page.

How can I get record id using that URL. Eg: I am getting Salesforce browser URL like: https://ap1.salesforce.com/0019000000GJMHG using this URL How can I get Salesforce record id through controller and display the record id in VF page. Like : Record Id: 0019000000GJMHG Can anyone please help me to overcome this issue?

share|improve this question
    
One thing to be aware of is that because referer information can violate privacy, some web browsers allow the user to disable the sending of referer information. Don't rely on this referral information as it's possible that it will not be present. If it is critical to know the ID from the previous page, pass it along as a URL parameter in the link. –  Mark Pond Dec 10 '12 at 21:54
add comment

2 Answers 2

up vote 0 down vote accepted

I guess you can use the string methods now to pull the ID.

BrowserURL=BrowserURL.right(15);//Pull from right the 15 digit Id

If your org is not update to winter 13 then you can use String.Split('/') and get the last array element value .

share|improve this answer
1  
This won't work if there are any get params in the URL, specifically the retURL param will be present on a large number of pages. –  ca_peterson Nov 27 '12 at 1:27
add comment

I've done this in javascript in the past and it boiled down to two regular expressions:

for the / URL format:

/\.com\/\w{15}|\.com\/\w{18}/

and for the ?id= format:

/id\=\w{15}|id\=\w{18}/

It's not foolproof - any VF page can use custom parameter names, but it covers all standard UI pages I'm aware of any any visualforce that uses a StandardController.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.