notes on board

Get Community/Digital Experience Site Base URL in Apex Dynamically

If you are using Salesforce Community and you have network Id of the community but I don’t know how to get Base URL of the community. Here is a way to get the community base URL using its id in apex:

  public static String getCommunityURL(String communityId){       
        String base = Network.getLoginUrl(communityId);
        base = base.replace('/login', '');
        return base;


Id communityId = Network.getNetworkId();