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?
@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();
}
}