Cloud

Why URLFOR() is an Admin’s Best Friend in Communities

Using URLFOR() and $Actions in a Custom Button

If you read my recent blog about how to customize your Community URL, you already know that the URL Salesforce uses for Communities differs from the native Salesforce URL that internal users are used to seeing. Due to this difference, it is critical that you setup all hyperlinks in the Community correctly if using Force.com to build your community. This is especially important if you have internal users accessing external communities or one community user accessing multiple communities.

It is a well-known best practice to always write links and buttons using relative URLs. However, relative URLs can be problematic in Communities if you do not use the right syntax. If you use the syntax “/{!Case.AccountId}” in a custom link, the link will break inside the Community as it does not pass on the /communityname/ part of the base URL when the user clicks the link. For example, if your base url is https://mycompany.force.com/communityname/, the link will take you to https://mycompany.force.com/AccountId (which will render an error message) instead of https://mycompany.force.com/communityname/AccountId, which will display your Account page.  

Using URLFOR() and $Actions in a Custom Button

Using URLFOR() and $Actions in a Custom Button

To prevent this error, use the URLFOR () function plus the $Action for the record you want to navigate to. URLFOR() determines what your base URL should be and the $Action determines what view of a record to go to (the view page, edit page, etc.). For example, to view an account, you write custom link as such: {!URLFOR($Action.Account.View, Case.AccountId, null, true)}. You will use the URLFOR () function in all your home page links, custom links, custom buttons and Visualforce pages that will be viewed in a Community by an internal or external user. Given the growing popularity of Communities, I recommend that administrators move away from the old syntax for writing relative URLs and to move to using URLFOR () everywhere so that if a Community (or Salesforce Console which also uses a unique base URL structure) is ever implemented, you will not need to update your URLs.

Note: Email templates and formula fields that contain hyperlinks do not support the URLFOR () function and should be paired with a custom label containing the base community URL for ease of ongoing administration. Please vote for my idea to support URLFOR() in formula fields!

About the Author

More from this Author

Thoughts on “Why URLFOR() is an Admin’s Best Friend in Communities”

  1. Kara,

    Do you have any suggestions on how to hand a community user copying and pasting a url and sending it to an internal user. When the internal user clicks on the link, it takes them to the community login page if they have not yet “Switched” into the community at least once during there session. If they have switched into the community at least once, it takes them directly to the record but in the community view.

    Denis

  2. Kara Allen Post author

    Denis, what’s the use case behind users sending a link? Without knowing what it is, I’ll throw out a few thoughts. You could change the way the communication is done to solve the problem. For example, rather than sending an email with a URL, move the conversation from email to Chatter, which would allow the internal user to respond to the communication in their Chatter feed. Or, you could create a way for the community user to send an email from inside the page in the Community that would automatically populate both the link inside the Community as well as the internal URL in the body of the email.

  3. Hi Kara,
    Your article is really helpful and gives information which is hard to find.

    I tried what you suggested and it almost worked, except that it continously reloads VF page again and again infinitely. I am not sure how can I stop this behavior. Do you have any idea?

    Thanks,
    Bakul

  4. I found different way to use URLFOR which solved my problem:

    {!URLFOR( $Site.BaseRequestUrl & “/apex/page_name?id=” & Opportunity.Id,Opportunity.Id, null, false)}

    Replace page_name with actual name of the page you want to show

  5. Kara Allen Post author

    Hi Bakul, I have never seen that behavior. Based upon your post, it seems like you have found a solution to this!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories