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
Bob Lee 14Bob Lee 14 

How to get current site id and details in apex?

Best Answer chosen by Bob Lee 14
Ajay K DubediAjay K Dubedi
Hi Bob,

You can use the below code and links: 

I have created an apex class as per your requirement.
 
public class SiteId {
     public static void getSideId(){
        String siteId = Site.getSiteId();
        Site site = [SELECT GuestUserId, Name, Subdomain, UrlPathPrefix FROM Site WHERE id = : siteId];
    }       
}

Links:

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

2.https://salesforce.stackexchange.com/questions/7639/get-site-url-from-apex. 
 


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Bob,

Greetings to you!

You can query the Id like this:
Site mySite = [select Id from Site where Name = 'MySite'];

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_site.htm

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_sitedetail.htm

You can get the secureURL using the SiteDetail object:
Site mySite = [select Id from Site where Name = 'MySite'];
SiteDetail mySiteDetail = [select SecureURL from SiteDetail where DurableId = :mySite.Id];
System.debug(mySiteDetail.SecureURL);

Please refer to the below link which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/7639/get-site-url-from-apex

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Bob,

You can use the below code and links: 

I have created an apex class as per your requirement.
 
public class SiteId {
     public static void getSideId(){
        String siteId = Site.getSiteId();
        Site site = [SELECT GuestUserId, Name, Subdomain, UrlPathPrefix FROM Site WHERE id = : siteId];
    }       
}

Links:

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

2.https://salesforce.stackexchange.com/questions/7639/get-site-url-from-apex. 
 


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

 
This was selected as the best answer
Donna GarzaDonna Garza
In Salesforce Apex, you can retrieve the current site's ID and details using the `Site` object. Here's how you can do it:

1. **Retrieve Current Site ID:**

You can get the current site's ID using the `Site.getSiteId()` method. Here's an example:

```apex
String siteId = Site.getSiteId();
System.debug('Current Site ID: ' + siteId);
```

2. **Retrieve Current Site Details:**

To retrieve details about the current site, you can query the `Site` object based on the site's ID. Here's an example:

```apex
String siteId = Site.getSiteId();
Site currentSite = [SELECT Id, Name, Subdomain, UrlPathPrefix FROM Site WHERE Id = :siteId LIMIT 1];
System.debug('Current Site Name: ' + currentSite.Name);
System.debug('Current Site Subdomain: ' + currentSite.Subdomain);
System.debug('Current Site URL Path Prefix: ' + currentSite.UrlPathPrefix);
```

Remember to handle potential exceptions that might occur, such as if the `Site` object is not available or if the query doesn't return any results.

Please note that to use this code, you need to have the necessary permissions and context to access the `Site (https://www.capcutgeeks.com/)` object. Also, make sure you are working in the appropriate context, such as a Visualforce page or a trigger, where the `Site` object and related methods are available.