+ Start a Discussion
LinThawLinThaw 

How to get community URL?

Hi,

I want to get community URL from apex class.
Is there anyway to get it?

I have been try the following, but I can't get absolute URL.

test -1- the following source only work when debug in developer console.
Network myNetwork = [SELECT Id FROM Network WHERE Name ='myCommunityName' ];
ConnectApi.Community  myCommunity = ConnectApi.Communities.getCommunity(myNetwork.id);
System.debug('MyDebug : ' + myCommunity.siteUrl);

test -2- the following result have extra word /login.
Network myNetwork = [SELECT Id FROM Network WHERE Name ='myCommunityName' ];
System.debug('MyDebug: ' + Network.getLoginUrl(myNetwork.id);
GauravGargGauravGarg
Hi Lin,

Please store Community URL into a custom setting or Custom Label, then access it in apex class. 
Access Custom Label in Apex:
 
String test = Label.<custom label name>;

for example:

String test = Label.ok;

To access Custom setting, please find below links:
Access Custom Setting : 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm


Hope this will help you, let me know if you still face issues in it. 

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Skype: gaurav62990
sandhya reddy 10sandhya reddy 10
Hi Lin,


SELECT Domain,DomainType FROM Domain

SELECT Name,UrlPathPrefix FROM Network WHERE Name ='myCommunityName'


You will get the domain and then all communities and you can build your logic.

you can even use 
ConnectApi.Communities.getCommunities() This works from developer console as well as from apex class, but you must use API 28.0 (or above).

Hope this information is helpful for you.

Please let us know if this helps you.

Thanks and Regards
sandhya
 
LinThawLinThaw
Thanks sandhya,

SELECT Domain,DomainType FROM Domain
This soql is best to extract domain address but how can I get if there is more than one domain?

SELECT Name,UrlPathPrefix FROM Network WHERE Name ='myCommunityName'
I only get the name of domain but I want the URL of domain, is there any continue?

Regards,
LinThaw.
LinThawLinThaw
Thanks GauravGarg,

If there is no way to get URL,
I will use Custom Label way.

Regards,
LinThaw.
sandhya reddy 10sandhya reddy 10
Hi Lin,

After some google search i found that network Sobject stores all communties.

As u did before

test -2- the following result have extra word /login.
Network myNetwork = [SELECT Id FROM Network WHERE Name ='myCommunityName' ];
System.debug('MyDebug: ' + Network.getLoginUrl(myNetwork.id);

You need to split your url as per your requirements.

And also there is something below which gives the base url.

Please refer below link which may help you.

URL.getSalesforceBaseUrl().toExternalForm()

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm

Please let us know if this helps you.

Thanks and Regards
sandhya
Bv1 TriggersBv1 Triggers
Hi Lin,

test -1- the following source only work when debug in developer console.
Network myNetwork = [SELECT Id FROM Network WHERE Name ='myCommunityName' ];
ConnectApi.Community  myCommunity = ConnectApi.Communities.getCommunity(myNetwork.id);
System.debug('MyDebug : ' + myCommunity.siteUrl);
 
The above code worked for only system admin,but not for users.User face the issue 'ConnectApi.NotFoundException: Resource not found. Class.ConnectApi.Communities.getCommunity' . Kindly advise..
Mike EMike E
You have the correct approach, you are just missing the removal of the /login from the end.

Ideally create a helper method in a class somewhere that'll give you the base URL given a community name. Something like the following, although it could be better. Ideally add some null checking, or if somehow it might return multiple communities with the same name.

public static String getCommunityBaseUrl (String communityName) {
    List<Network> communities = [SELECT Id, Name FROM Network WHERE Name = :communityName];
    return Network.getLoginUrl(communities[0].Id).removeEnd('/login');
}
LinThawLinThaw
Thanks Mike,
It is not bad using removeEnd.
Suraj Tripathi 47Suraj Tripathi 47
Hi Lin,
Greetings!

Community URL can be fetched by SOQL Query in Network.
Please replace YourCommunityName with your community name.
String communityName = 'YourCommunityName';
SELECT Name, UrlPathPrefix FROM Network WHERE Name = :communityName;

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi