I saw how to implement public REST API with Salesforce,

Can I have a public APEX REST API?


The problem is that when i want to expose data, i run soql on Contact object and i did not retrieve any data!

As I see in this documentation: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_exposing_data.htm the it is run as a system context and i should retrieve data :(

Did someone suceed to expose Data from object in a public rest webservice? what I should do in the Site exposing the rest web service?


global with sharing class MenuService {

 global static String getMenuTree() {
    RestResponse res = RestContext.response;
    res.addHeader('Access-Control-Allow-Origin', '*');
    res.addHeader('Content-Type', 'application/json');
    String contactId = RestContext.request.params.get('contactid'); 

    JSONGenerator generator = JSON.createGenerator(true);
    if(contactId!=null && (contactId.length() == 15 || contactId.length() == 18) && Pattern.matches('^[a-zA-Z0-9]*$', contactId)) {
        Contact[] contacts = [Select Id,Name from Contact where Id=:contactId];
            generator.writeStringField('Name','Unknow contact');
    //... other logic


    return generator.getAsString();


1 Answer 1


Could you please post your code. I think problem in code signature or modification.

With sharing modificator used for apply current sharing model to you class. You can read docs about it - https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm

  • I added a part of the code
    – Akram G
    Apr 10, 2015 at 8:50
  • 1
    Try to remove with sharing modificator
    – Gres
    Apr 10, 2015 at 8:54
  • 1
    That solved my problem! can you explain why? and then edit your original post?
    – Akram G
    Apr 10, 2015 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .