This is a very simple rest API developed to lookup and return a single preference record – the temperature preference. This is intended as a proof of concept and is exposed via Sites as an anonymous rest API and has been deployed and tested in a development org. There would be a lot of work to complete to make this production ready but it demonstrates the key functionality of setting the temperature via a guest preference stored in Salesforce.
@RestResource(urlMapping=’/guest/*’)
global class tempController {
@HttpGet
global static void /*string*/ getGuestPreference() {
//First set a default temp
string guestPreferenceTemp = ’76’;
integer firsttemp;
//Looks up the first guest preference
//Would need to find the specific guest in production
List<guestPreferences__c> tempPref= [SELECT temp__c from guestPreferences__c];
if(tempPref.size()>0){
firsttemp = integer.valueof(tempPref[0].temp__c);
}
if(firsttemp == null){
system.debug(‘no temp found for user’);
}else{
JSONGenerator generator = JSON.createGenerator(true); //instantiation of the generator
generator.writeStartObject(); // Writes the starting marker of a JSON object ‘{‘
generator.writeStringField(‘preftemp’, string.valueof(firsttemp));
generator.writeEndObject(); //Writes the ending marker of a JSON object ‘}’
guestPreferenceTemp = generator.getAsString();
}
//return guestPreferenceTemp;
RestContext.response.addHeader(‘Content-Type’, ‘application/json’);
RestContext.response.responseBody = Blob.valueOf(guestPreferenceTemp);
}
}