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