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
Yassine ZouagYassine Zouag 

How to write global variables in an APEX controller class ?

Hello,
I want to use a global variable ($Site.Prefix) in an APEX controller class, inside a condition to determine which subdomain i'm using.
For example : 
urlPath = {!$Site.Prefix};
However, I'm getting : 
Error: Compile Error: unexpected token: '{' at line ...

Since I cannot use APEX tags in my APEX class, do you know any workaround ?

Thanks.
Best Answer chosen by Yassine Zouag
Arunkumar RArunkumar R
Hi Yassine,

You need to change the code according to your class/api version,

Use the below syntax for API Version 29.0 or above
urlPath = Site.getPrefix();

Use the below syntax for API Version 30.0 or above
urlPath = Site.getPathPrefix();

Refer from the below site for more details,

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

All Answers

Shweta_AgarwalShweta_Agarwal
Hi,

You can use from apex: "Site.getPrefix()"
So your code will be 

urlPath = Site.getPrefix();

Thanks
Arunkumar RArunkumar R
Hi Yassine,

You need to change the code according to your class/api version,

Use the below syntax for API Version 29.0 or above
urlPath = Site.getPrefix();

Use the below syntax for API Version 30.0 or above
urlPath = Site.getPathPrefix();

Refer from the below site for more details,

http://​https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sites.htm
This was selected as the best answer