In this blog we are going to learn about how to get site URL in salesforce apex class according to salesforce org type . We can also list the community sites in salesforce. Let’s see an example.

Example

In this example we are going to list down all active sites available in salesforce org.

For sandbox site url we need to add the sandbox name prior to sub-domain of the site. We can get the sandbox name from the username which we get from UserInfo class.

Note : Above method is not applicable for customised username. Means you change the username, output might be different according to changes.

–>Format of sandbox site url is : “http://{sandbox-name}–{subdomain}.{salesforce-instance}.force.com”

–>Format of enterprise edition site url is : “http://{subdomain}.{salesforce-instance}.force.com”

–>Format of developer edition site url is : “http://{subdomain}-developer-edition.{salesforce-instance}.force.com”

#Step 1 : Create a apex class “siteurl” and paste the following code.

public  with sharing  class siteurl{
    /**
        * Webkul Software.
        *
        * @category Webkul
        * @author Webkul
        * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
        * @license https://store.webkul.com/license.html
        */
    public List getSiteUrl(){ 
        List siteList = [SELECT GuestUserId, Name,MasterLabel, Subdomain, 
        OptionsRequireHttps, UrlPathPrefix FROM Site WHERE Status = 'Active' Limit 1000];
        
        List siteFullUrlList = new List();
        /** We can get instance of the org from organisation object **/
        Organization org = [SELECT InstanceName,Name, IsSandbox, OrganizationType FROM Organization];
        if(siteList != null && siteList.size() != 0) {
            for(Site s: siteList) {
                if(s.Subdomain != null) {
                    String httpStr = 'http://';
                    if(s.OptionsRequireHttps == true) {
                        httpStr = 'https://';
                    }
                    String siteFullUrl = httpStr;
                    if(org.IsSandbox == true) {
                        siteFullUrl += UserInfo.getUserName().substringAfterLast('.')+'-';
                    }
                    siteFullUrl += s.Subdomain + '.';
                    siteFullUrl += (org.IsSandbox || org.OrganizationType == 'Developer Edition' ? (org.InstanceName.toLowerCase() + '.') : '') + 'force.com';
                    if(s.UrlPathPrefix != null) {
                        siteFullUrl += '/'+s.UrlPathPrefix; 
                    }
                    siteFullUrlList.add(siteFullUrl);
                }
            }
            
        }  
        return siteFullUrlList;
    }
}

#Step 2 : Create visualforce page with following code.

<apex:page controller="siteurl">
    <!--/**
         * Webkul Software.
         *
         * @category Webkul
         * @author Webkul
         * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
         * @license https://store.webkul.com/license.html
         */
      -->
      <apex:repeat value = "{!siteurl}" var="s">
            {!s}<br/>
      </apex:repeat>
</apex:page>

Output

# Site URL List in Developer Edition.

# Site URL List in sandbox .

Support

That’s all about how to get site URL in salesforce, still if you have any further query, feel free to contact us, we will be happy to help you
https://wedgecommerce.com/contact-us/

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home