2

I saw how to implement public REST API with Salesforce,

Can I have a public APEX REST API?

https://developer.salesforce.com/blogs/developer-relations/2012/02/quick-tip-public-restful-web-services-on-force-com-sites.html

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?

@RestResource(urlMapping='/getmenu/*')

global with sharing class MenuService {



   @HttpGet
 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);
    generator.writeStartObject();
    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];
        if(contacts.size()>0){
             generator.writeStringField('Name',contacts[0].name);
        }else{
            generator.writeStringField('Name','Unknow contact');
        }
    }
    //... other logic

    generator.writeEndObject();

    return generator.getAsString();
    }

}

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 '15 at 8:50
  • 1
    Try to remove with sharing modificator – Gres Apr 10 '15 at 8:54
  • 1
    That solved my problem! can you explain why? and then edit your original post? – Akram G Apr 10 '15 at 9:06

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.