在Apex中获取Site URL

Foreword

目前SF暂未提供直接有效的方法在Apex获取SiteURL,我们可以在Idea (Access URL for a Site or Community from Apex)页面投票,除了下面提供的一种hack思路,当然也可以通过Custom Label手动维护。

Format of Site URL

Sandbox site url:
“http://{sandbox-name}–{subdomain}.{salesforce-instance}.force.com”
Enterprise edition site url:
“http://{subdomain}.{salesforce-instance}.force.com” Format of
Developer edition site url:
“http://{subdomain}-developer-edition.{salesforce-instance}.force.com"

Customization

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>

Final Effect

Site URL List in Developer Edition

在这里插入图片描述
在这里插入图片描述

Site URL List in sandbox

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值