Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Created March 7, 2024 07:11
Show Gist options
  • Save TheShubhamVsnv/d16b08fce4c902aef53e37b0a043a43b to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/d16b08fce4c902aef53e37b0a043a43b to your computer and use it in GitHub Desktop.
//PUT: Update existing data or resources in Salesforce.
@future(callout=true)
public static void putContact() {
HttpRequest req = new HttpRequest();
// Use a more meaningful endpoint URL
req.setEndpoint('callout:SourceOrgNamedCredential/services/apexrest/Contact/003F900001gMifVIAS');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
// Use a map to represent the request body and serialize it
Map<String, String> requestBodyMap = new Map<String, String>{
'contactFirstName' => 'Steven',
'contactLastName' => 'Foster'
};
String requestBody = JSON.serialize(requestBodyMap);
req.setBody(requestBody);
req.setMethod('PUT');
Http http = new Http();
HttpResponse response = http.send(req);
// Error handling
if (response.getStatusCode() >= 200 && response.getStatusCode() < 300) {
system.debug('Contact updated successfully');
system.debug('Response body: ' + response.getBody());
} else {
system.debug('Error updating contact. Status code: ' + response.getStatusCode());
system.debug('Response body: ' + response.getBody());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment