23

Question

If I'm on a managed package page, how can I link to a visualforce page in the default namespace without hardcoding the instance?

Background

I'm building a skuid page that has a custom action that sends users to a visualforce page. If I specify the relative URL for the visualforce page, e.g. /apex/MyPage, the redirect stays in the skuid namespace, i.e. https://skuid.<pod>.visual.force.com/apex/MyPage, which gives a URL not found error.

I'm able to get things working if I specify the absolute url, i.e. https://<pod>.salesforce.com/apex/MyPage, but I'm hoping to avoid hardcoding the absolute URL.

I've searched through all the Skuidify manuals and haven't been able to find any solutions there. Hoping the community has some tips here.

5
  • Hopefully @zachelrath will cruise by and drop a helpful code snippet.
    – Mark Pond
    Commented Dec 11, 2013 at 21:13
  • I think its a nuance of using relative paths from within managed packages as opposed to anything skuid related, I think you always have to specify the absolute/whole URL Commented Dec 12, 2013 at 17:13
  • @PhilHawthorn You definitely do NOT need to specify the absolute URL --- that is bad practice and will run you into lots of trouble. see reply from @SkuidJohn below. /apex/c__MyPage will always load the VF Page called MyPage within the local namespace, and /apex/ns__MyPage will always take you to a page called MyPage within the ns namespace.
    – zachelrath
    Commented Feb 3, 2014 at 17:27
  • @MarkPond Thanks for the vote of confidence -- however unwarranted :) Just realized you could subscribe to particular tags, so I've subscribed to "skuid".
    – zachelrath
    Commented Feb 3, 2014 at 17:28
  • My point was really it being a nuance of referencing pages within a managed package rather than anything specifically skuid related, if I'd have given it enough thought for an answer I would have done that. Thanks for pointing it out though. Commented Feb 3, 2014 at 18:04

3 Answers 3

40

I realize this is a little late in coming, but for posterity's sake, if you append the namespace to the front of your visualforce page name, salesforce will make sure it gets the proper namespace in the full url.

So if you want your action to go to /apex/MyPage, you would do this: /apex/c__MyPage

If you're linking to something in a managed package you need to use that namespace instead: /apex/mpnamespace__MyPage

Hope that helps!

PS: We should definitely add this to our docs. It shall be done.

3
  • 6
    Oh sweet, did not no that you could use c__ to specify the default namespace, this is VERY helpful! Thanks @skuidjohn Commented Feb 4, 2014 at 21:42
  • exactly, I wasn't aware of this as well that there is actually a "c__" default namespace that we can actually reference to custom vf pages.
    – VarunC
    Commented Jul 18, 2016 at 8:42
  • does that mean, that if i put url apex/c__MyPage to the button and then in managed package salesforce will figure out and change c__ to real namespace of the org?
    – Bogdan K
    Commented Sep 22, 2016 at 16:21
5

Use the URLFOR function in conjunction with the $Page global variable. e.g. action="{!URLFOR($Page.Your_VF_Page_Name,[params])}"

Another way of doing this is returning a PageReference to Page.Your_VF_PageName in a controller.

Yes, you can pass in parameters using this technique. Yes, it will resolve your namespace correctly.

In my opinion, using the Page global variable is a much better practice than hardcoding the namespace in the URL or using the getURL method for constructing the URL.

For more info on using the $Page global variable, see this blog post: http://www.fishofprey.com/2011/12/safe-urls-for-use-with-salesforce.html

1
  • 1
    Totally accurate, unfortunately in this scenario I'm just passing the url into a 3rd party package, so I don't have the ability to use URLFOR, fortunately @skuidjohn found the solution by using c__ to specify the standard namespace. Commented Feb 4, 2014 at 21:44
2

Try //login.salesforce.com/apex/MyPage

In general, you can substitute login.salesforce.com for the specific instance and it will redirect you to the correct url.

Edit:

If you want to use this in a sandbox as well, you can use:

//test.salesforce.com/apex/MyPage

However, you'll need to add logic to determine if you're in production or a sandbox. If you're not an ISV and are just building this for a deterministic production org, you can use the code snippet below to determine if the code is executing on the production org:

public Boolean isSandbox() {
    return URL.getSalesforceBaseUrl().getHost().left(2).equalsignorecase('cs');
}

Snippet modified from the original at this Salesforce success discussion.

5
  • Is that going to work on sandboxes thought? I'm not particular concerned about the production case since it's very rare pods switch there, but sandboxes will nearly always be on different pods ... Commented Dec 11, 2013 at 23:33
  • Good point. You'd need to change the url to //test.salesforce.com/apex/MyPage to get it to work in a sandbox. However, you can't have it work in both production and sandbox at the same time unless you put logic in to understand if it's a sandbox. I'll update my answer with another suggestion
    – Akrikos
    Commented Dec 12, 2013 at 15:26
  • Thanks for all the suggestions, voted it up, but unfortunately this isn't an apex class, so I can use that method. Commented Dec 12, 2013 at 21:09
  • I'm not sure what your circumstance is, but you could probably use a controller extension to allow you to access custom apex in your VF page: salesforce.com/us/developer/docs/pages/Content/…
    – Akrikos
    Commented Dec 13, 2013 at 15:25
  • Yeah, unfortunately it's a config setting in a managed package. I don't have that option, at this point it sounds like I'm stuck with hard-coding the absolute URL. Commented Dec 16, 2013 at 23:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .