-
-
Save TheShubhamVsnv/b4661b5bd744ebdd9582ab9142ad1589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource (urlMapping='/Contact/*') | |
global class ContactAPI { | |
// Define the HTTP GET method | |
@HttpGet | |
global static List<Contact> getContacts() { | |
// Retrieve specific fields for up to 10 contacts | |
return [SELECT Id, Name, Email, Phone FROM Contact LIMIT 10]; | |
} | |
// Define the HTTP POST method | |
@HttpPost | |
global static Contact createContact(String contactFirstName, String contactLastName) { | |
Contact con = new Contact( | |
FirstName = contactFirstName, | |
LastName = contactLastName | |
); | |
try { | |
insert con; | |
} catch (Exception e) { | |
// Handle exception during contact creation or sending | |
System.debug('Error: ' + e.getMessage()); | |
} | |
return con; | |
} | |
// Define the HTTP PUT method | |
@HttpPut | |
global static string putContact(String contactFirstName, String contactLastName) { | |
RestRequest req = RestContext.request; | |
// Use more meaningful variable names | |
String contactId = req.requestURI.substring(req.requestURI.lastindexof('/') + 1); | |
// Add error handling for query | |
Contact con; | |
try { | |
con = [SELECT Id FROM Contact WHERE Id = :contactId LIMIT 1]; | |
} catch (Exception e) { | |
return 'Error: ' + e.getMessage(); | |
} | |
// Update contact fields directly without querying | |
con.FirstName = contactFirstName; | |
con.LastName = contactLastName; | |
update con; | |
// Return a JSON response instead of a concatenated string | |
return JSON.serialize(con); | |
} | |
// Define the HTTP PATCH method | |
@HttpPatch | |
global static String patchContact(String contactFirstName, String contactLastName) { | |
RestRequest req = RestContext.request; | |
String contactId = req.requestURI.substring(req.requestURI.lastindexof('/') + 1); | |
try { | |
// Fetch the contact record | |
Contact con = [SELECT Id, FirstName, LastName FROM Contact WHERE Id = :contactId LIMIT 1]; | |
// Update contact fields based on provided parameters | |
if (contactFirstName != null) { | |
con.FirstName = contactFirstName; | |
} | |
if (contactLastName != null) { | |
con.LastName = contactLastName; | |
} | |
// Perform the update | |
update con; | |
// Return the updated contact record | |
return JSON.serialize(con); | |
} catch (Exception e) { | |
return 'Error updating contact: ' + e.getMessage(); | |
} | |
} | |
// Define the HTTP DELETE method | |
@HttpDelete | |
global static string deleteContact() { | |
// Retrieving the incoming REST request | |
RestRequest req = RestContext.request; | |
// Extracting the contact Id from the request URI | |
String contactId = req.requestURI.substring(req.requestURI.lastindexof('/') + 1); | |
// Querying for the contact record to be deleted | |
Contact contact = [SELECT Id FROM Contact WHERE id = :contactId LIMIT 1]; | |
// Deleting the contact record | |
delete contact; | |
// Returning a success message | |
return 'Success Deletion'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment