Get Salesforce Base URL in Visualforce Page

Apex Class:

public class SampleBaseURL{

    public List<Account> accList {get;set;}
    
    public SampleBaseURL(){
        accList = [SELECT Id, Name FROM Account LIMIT 100];
    }
}

Visualforce Page:

<apex:page controller="SampleBaseURL" sidebar="false">
    <apex:repeat value="{!accList}" var="a">
        <a href="{!$Site.BaseUrl}/{!a.Id}" target="_blank">{!a.Name}</a>
    </apex:repeat>
</apex:page>

Output:

  • subhasini

    hi {!$Site.BaseUrl} is working when user is logged in a community user.
    but it is not working when non community user is creating a record