function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramk123Ramk123 

Apex logic help to switch communities

Dear Experts,

I would like to switch community with the apex logic.

SELECT GuestUserId, Name, Subdomain, UrlPathPrefix FROM Site 'abc'

If UrlPathPrefix is abc
returnurl = '/abc' + returnurl
else if UrlPathPrefix  is xyz
returnurl = '/xyz' + returnurl

anyone please help me to frame the logic to return proper community URL.
SwethaSwetha (Salesforce Developers) 
HI Santosh,
Using Network.getLoginUrl('Pass Community id here') you can get the login page URL for the community and then break the URL and use it as per requirement.

Related: https://salesforce.stackexchange.com/questions/55506/how-can-i-get-my-community-base-url-in-apex

If this information helps, please mark the answer as best.Thank you
AnudeepAnudeep (Salesforce Developers) 
Hi Santosh, 

Posting sample code here for your reference
 
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;
    }
}

NOTE: The code provided is an example. You'll need to review and make modifications for your organization.

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you