Skip to content

Instantly share code, notes, and snippets.

@amitastreait
Last active April 11, 2019 11:12
Show Gist options
  • Save amitastreait/2bf1ab0977788ee11ca8a1a33cec1f95 to your computer and use it in GitHub Desktop.
Save amitastreait/2bf1ab0977788ee11ca8a1a33cec1f95 to your computer and use it in GitHub Desktop.
Introduction to Apex Rest with Examples
@RestResource(urlMapping='/v1/BookManagement/')
global class BookManager {
@httpGet
global static Book__c doGetBook(){
Book__c book = new Book__c();
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
book = [Select Id, Name, Price__c From Book__c Where Id =: bookId];
return book;
}
@httpDelete
global static String doDeleteBook(){
Book__c book = new Book__c();
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
book = [Select Id, Name, Price__c From Book__c Where Id =: bookId];
delete book;
return 'Record Deleted!';
}
@httpPost
global static Book__c doCreateBook(BooksInfo bookInfo){
Book__c book = bookInfo.book;
insert book;
return book;
}
@httpPut
global static Book__c doUpdateBook(String Name){
Map<String, String> paramsMap = RestContext.request.params;
String bookId = paramsMap.get('Id');
Book__c book = new Book__c(Name = name, Id = bookId);
update book;
return book;
}
global class BooksInfo{
Book__c book { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment