Loading

Get Community ID using Apex

Veröffentlichungsdatum: Jun 25, 2026
Beschreibung

When building Apex code that needs to identify specific Experience Cloud sites (formerly known as Communities), you can retrieve the unique Site ID for each site programmatically. The ConnectApi.Communities.getCommunities() method returns a list of all Experience Cloud sites the running user has access to, including their IDs, names, and URLs.

When to Use This

Retrieving Experience Cloud Site IDs programmatically is useful in the following scenarios:

  • Routing users to a specific Experience Cloud site based on their profile or context
  • Generating site-specific URLs for links or redirects in Apex code
  • Filtering data by site membership in SOQL queries that reference site IDs
  • Debugging or verifying which Experience Cloud sites are accessible to a given user
Lösung

How to Retrieve Experience Cloud Site IDs Using Apex

Run the following statement in the Developer Console's Execute Anonymous window, or include it in a debug log for review. The statement calls the getCommunities() method from the ConnectApi.Communities class, which returns a CommunityPage object containing a list of Community objects.

System.debug('##### ' + ConnectApi.Communities.getCommunities());

After running this statement, open the debug log and search for #####. The output includes a list of ConnectApi.Community objects. Each object contains the following properties:

  • id: The unique ID of the Experience Cloud site (formerly Community ID)
  • name: The display name of the site
  • siteUrl: The public URL of the site

You can use the id value in subsequent Apex logic to reference or filter by a specific Experience Cloud site.

Nummer des Knowledge-Artikels

000386512

 
Laden
Salesforce Help | Article