I am using Salesforce Community and I have network Id of the community but I don't know how can I get Base URL of my community. Any one know how can I get my community base URL using its id in apex as a internal user.

Is there any other way in which salesforce internal user can get the base URL of all his community in the apex also i don't find any object in which Community Base URL is stored. I checked network sObject but it only store community path prefix not the complete URL so how can i get the base URL for my community?

3 Answers 3


finally i found the answer i use

Network.getLoginUrl('Pass Community id here')

in this way i found the login page URL for the community and then break the URL and use as per my Requirement.

  • If you do this, you get more than just the base URL, you get /s/login at the end of it. You can do string manipulation, but this approach seems less than reliable, as the login URL could change. I think Esteban's answer below seems more future-proof. Jan 5, 2021 at 20:17
  • @MatthewSouther He has hardcoded the community base URL. It is the same details which I am asking. how you will get the URL? If I store directly in custom setting then I can directly store the complete URL instead of doing all query and checks. Jan 6, 2021 at 6:48
  • @MatthewSouther for login its not like we will change it every other day. Jan 6, 2021 at 6:48

this is an approach that you can also take to prepare a base URL when clicking standard or CustomObjects links within a Lightning/VFP (for example). If you want to load those record Details page dynamically, opening on internal and Community properly, you can use something like this:

List<Network> network = [SELECT Name, UrlPathPrefix FROM Network WHERE Name = 'YOUR COMMUNITY NAME'];
String base = URL.getSalesforceBaseUrl().toExternalForm(); 
if (base.contains('https://yourCommunityDomain.force.com') && !network.isEmpty())
    baseURL = base + '/' + network[0].UrlPathPrefix + '/s/detail';
    baseURL = base;


  • I used a List because if you don't reach a Record you get empty list instead of an exception.
  • Base URL represents the URL that user is using (it will load for internal and external/Community URLs)
  • If it is a Community user access, then I prepare the URL by using the base external Community URL, then the URL you choose for your Community, and then the /s/detail in order to be able to open record Details by ID

Hope it helps, happy code!

  • I need the community URL here and you are hardcoding that value. I don' t think it will solve the issue. Aug 13, 2020 at 6:27
  • In fact, it solves the issue, is already working here ;) If you don't want to put the CommunityName in the code directly, you can use Custom Settings too :) The good part of this approach is that you can run internal / external URL accordingly. The code can be evolved if you want. Good luck! Aug 17, 2020 at 21:39

On a Visualforce page controller/extension, you can always grab the base URL using the URL class methods in apex:


URL Class documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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