0

We are using the following code to find the Secure URL for the public site by Apex

        List<String> siteFullUrlList = new List<String>();

        List<Site> siteList = [
            SELECT GuestUserId, Name, MasterLabel, Subdomain, 
                OptionsRequireHttps, UrlPathPrefix 
            FROM Site 
            WHERE Status = 'Active'
        ];

        Organization org = [
            SELECT InstanceName, Name, IsSandbox, OrganizationType 
            FROM Organization
        ];

        if(siteList != null && siteList.size() != 0) {
            for(Site s: siteList) {
                if(s.Subdomain != null) {
                    String siteFullUrl = (s.OptionsRequireHttps) ? 'https://' : 'http://';
                    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);
                }
            }
        }

however, it doesn't work, since the URL for the public site sometimes has 'secure.force.com' suffix while sometimes it doesn't have it and it is not clear when it has and what it doesn't have 'secure' subdomain in between.

How can we find the correct way to find the Secure URL?

0

There is SiteDetail object which contains the secure URL

    List<Site> siteList = [
        SELECT GuestUserId, Name, MasterLabel, Subdomain, 
            OptionsRequireHttps, UrlPathPrefix 
        FROM Site 
        WHERE Status = 'Active'
    ];


    List<SiteDetail> siteDetails = [SELECT DurableId, SecureUrl FROM SiteDetail where DurableId IN :siteList];
    Map<Id, String> secureURLMap = new Map<Id, String>();
    for (SiteDetail sd: siteDetails) {
        secureURLMap.put(sd.DurableId, sd.SecureUrl);
    }

    for(Site s: siteList) {
        siteFullUrlList.add(secureURLMap.get(s.Id));
    }
| improve this answer | |
  • 1
    if(siteList != null && siteList.size() != 0) { is not necessary. A qurty result is never null, and it is okay to iterate over an empty list. – sfdcfox Jun 3 at 14:57
  • @sfdcfox agree. Updated – Patlatus Jun 3 at 15:02
0

There is another way to achieve the desired result.

    List<String> siteFullUrlList = new List<String>();

    List<Site> siteList = [
        SELECT GuestUserId, Name, MasterLabel, Subdomain, 
            OptionsRequireHttps, UrlPathPrefix 
        FROM Site 
        WHERE Status = 'Active'
    ];

    Organization org = [
        SELECT InstanceName, Name, IsSandbox, OrganizationType 
        FROM Organization
    ];
    Map<Id, DomainSite> domainSites = new Map<Id, String>(); 
    List<DomainSite> domainSiteList =  [
        SELECT SiteId, Domain.Domain, PathPrefix
        FROM DomainSite 
        WHERE SiteId IN (SELECT Id FROM Site WHERE Status = 'Active')
    ];

    for (DomainSite siteItem: domainSiteList){
        domainSites.put(siteItem.SiteId, siteItem.Domain.Domain + siteItem.PathPrefix);    
    }

    for(Site item: siteList) {
        siteFullUrlList.add(buildSecureURLManually(item, org, domainSites.get(item.Id)));
    }    


private static String buildSecureURLManually(Site publicSite, Organization org, DomainSite siteItem) {
    String siteFullUrl = publicSite.OptionsRequireHttps ? 'https://' : 'http://';
    if (org.IsSandbox) {
        siteFullUrl += UserInfo.getUserName().substringAfterLast('.') + '-';
    }
    if (publicSite.Subdomain != null) {
        siteFullUrl += publicSite.Subdomain + '.';

        siteFullUrl += (org.IsSandbox || org.OrganizationType == 'Developer Edition' ? (org.InstanceName.toLowerCase() + '.') : '');
        siteFullUrl += !org.IsSandbox ? 'secure.' : '';
        siteFullUrl += 'force.com';
        if(publicSite.UrlPathPrefix != null) {
            siteFullUrl += '/' + publicSite.UrlPathPrefix;
        }
    } else {
       siteFullUrl += siteItem.Domain.Domain + siteItem.PathPrefix;
    }

    return siteFullUrl;
}
| improve this answer | |
  • I think this might not work for some specific cases where 'secure.' suffix is applied for some sandboxes which are refreshed from a production having the site enabled with secure URL – Patlatus Jun 9 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.