Access Visualforce Page without Login

In this post, we will implement the functionality to access Visualforce Page without Login using Sites in Salesforce. There might be a requirement where we need to expose some information for Customers or take some inputs from the Customer like Feedback. In such cases, we can expose Visualforce Page publically to show some information or to get some input from the Customer.

Implementation

In this implementation, we will create a Visualforce page that will show the Account details for a particular Account. This page will be accessible publically and any user can access this Visualforce Page without login.

First, create a Visualforce Page PublicPage to display some fields of Account like Name, Account Number, Phone, and Website. Use apex:outputField to display the fields.

PublicPage.vfp

<apex:page standardStylesheets="true" showHeader="false" controller="PublicController" >
    <apex:form>
        <apex:pageBlock title="Visualforce Page with PUBLIC Access (Without Authentication)">
            <apex:pageBlockSection title="Account Details" columns="1">
                <apex:outputField value="{!objAccount.Name}"/>
                <apex:outputField value="{!objAccount.AccountNumber}"/>
                <apex:outputField value="{!objAccount.Phone}"/>
                <apex:outputField value="{!objAccount.Website}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Then, create an Apex Class PublicController that will query the Account details. For the purpose of this implementation, I have hardcoded Id of the Account to query.

PublicController.apxc

public class PublicController {
    public Account objAccount {get; set;}
    
    public PublicController(){
        objAccount = [SELECT Name, AccountNumber, Phone, Website FROM Account WHERE Id = '0012x000008VEr5' LIMIT 1];
    }
}

Sites in Salesforce

Sites in Salesforce enables us to create Public Web Applications which are directly integrated with our Salesforce Organization. It does not require users to log in with username and password to see the pages hosted with Sites in Salesforce. Sites can be used to publically expose some information for Customers or take some inputs from the Customer like Feedback.

  • First, type Sites in the Quick Find box and click on Sites.
  • Add the Domain name for Site and click on Check Availability.
  • If it is available, then click on Register My Salesforce Site Domain.

It should look something like below:

Sites in Salesforce
Sites in Salesforce

Once the domain is registered, we need to create a new Site.

  • Click on New under Sites, Enter Label and Name.
  • For Active Site Home Page, select the Visualforce Page we created earlier which will be the Home page for our Site.
  • Check the Active Checkbox and keep the other fields as default. Click Save.

Once the Site is created, it should look like this:

Sites in Salesforce
Sites in Salesforce

This is enough to access Visualforce Page without Login using Sites in Salesforce. But if we are using Apex Controller or showing Salesforce records on Page, we need to provide appropriate access to Site Profile. For every Site, a Profile is created.

  • Go to the Site page, and click on Public Access Settings. This will open the Site Profile.
  • Provide the access to Apex Class that we created earlier.
  • Provide Read access to Account Object and necessary fields if required.

That is all we need.

Access Visualforce Page without Login

We can just open the Site Url in the Browser. Under the Custom URLs section, copy the Domain URL and paste it in Browser. It should open the Visualforce Page without Login.

Site Domain URL:

https://niksdevsite-developer-edition.ap17.force.com/
Access Visualforce Page without Login
Access Visualforce Page without Login

This is how we can access Visualforce Page without Login using Sites in Salesforce.

If you don’t want to miss new posts, please Subscribe here. If you want to know more about Sites in Salesforce, check Salesforce official documentation here.

Some handpicked topics for you:

See you in the next implementation.