I wish to use the methods of Apex classes in site.com. Can someone suggest if it is possible? And if not, are there any workarounds ?

I have tried searching a lot but all the information I can find explains how to call apex methods from apex pages, which is not my requirement. My requirement is that i wish to call the apex methods from site.com

  • Note - question has been duplicated on StackOverflow: stackoverflow.com/questions/21814953/… – Daniel Ballinger Feb 17 '14 at 0:08
  • True, but this is the better place for this question. – Tony Brasunas Dec 4 '14 at 1:09
  • When calling the Apex class method, who should it execute as? The Sites users won't be authenticated. Do you see them authenticating with Salesforce credentials or having some sort of system account that will be used? With the latter, keeping the credentials secure will be an important consideration. – Daniel Ballinger Dec 4 '14 at 8:04
  • Thanks for the question. It doesn't matter exactly who it executes as. The thing is the site.com pages will be public-facing, so it needs to be able to execute as the site.com profile user, or some other way that works really regardless of who actually happens to be viewing the page. – Tony Brasunas Dec 4 '14 at 21:31
  • Well, it looks like there isn't yet a clear and elegant answer to this question. Perhaps Site.com will roll out something better integrated with underlying apex functions and methods. Both Daniel and Jigar's answers below offer useful guidelines. I'm giving Jigar the bounty although Daniel's link to the Site.com Data Services page is very useful as well. – Tony Brasunas Dec 10 '14 at 19:26

The basic Site.com Data Services allow for

basic retrieval of data to be displayed in site.com pages and the form elements allow for simple collection of information from site visitors to be stored in Standard or Custom Objects. Source - mark-pond

If that doesn't give you sufficient access to achieve what you want then you could expose an Apex method via REST. The trick where will be how you get a Salesforce Session ID to authenticate your calls with. The OAuth 2.0 User-Agent Flow would be a good starting point.

If you do go with Apex REST web service methods note the warning about object permissions and field-level security here.


There are multiple ways to do this.

  1. Create an Apex REST service to expose the Apex written business logic as an endpoint and then use Javascript snippet in the Site.com page to call the Url endpoint. You can authenticate either using the OAuth flow or simply pass the current session ID using {!$Api.Session_ID} in the request header. Oauth would be a better way as it is more secured compared to the session id approach.
  2. Visualforce or Javascript Remoting may be one way to accomplish this where you would be able to invoke a method written in an apex class from Javascript. Mark the method in the Apex class with the @RemoteAction annotation.
  3. Create an intermediate Visualforce page which is basically a switcher. This page would have a controller. Invoke the actual method in the constructor of the switcher class. This approach would not be viable if you need to perform a DML as DML operations are not allowed in the constructor.
  • Would method #2 -- Javascript Remoting -- be possible from within Site.com? If so, that would probably be the best way to do this, as we already utilize JS Remoting in another part (visual force) of our application. – Tony Brasunas Dec 5 '14 at 23:46
  • You should be able to use Javascript Remoting only in visualforce pages or components hence these are the possible ways to proceed. - Use an iframe to include a visualforce page which can then leverage Visualforce remoting. If iframe seems a bad way you would need to have the html page redirect to an intermediate VF page which contains the Visualforce Remoting call. Let me know if this helps !! – Jigar Shah Dec 7 '14 at 5:32
  • The iframe solution wouldn't be workable, as I don't believe there's a way to communicate across the iframe to the page. It would need to utilize CORS and would just be a really lot of code for something that I believe should be simple. Call JS Remoting from the site.com page would be ideal, but I guess that isn't possible. – Tony Brasunas Dec 8 '14 at 19:31
  • This information is useful here too: help.salesforce.com/apex/… – Tony Brasunas Dec 10 '14 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.