Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Created March 7, 2024 07:10
Show Gist options
  • Save TheShubhamVsnv/9bf6fdaf4b1ff71da3c39b27fa38ab9a to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/9bf6fdaf4b1ff71da3c39b27fa38ab9a to your computer and use it in GitHub Desktop.
@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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment