Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Created March 7, 2024 08:03
Show Gist options
  • Save TheShubhamVsnv/798894802627ff91c0c0f330a196c0ef to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/798894802627ff91c0c0f330a196c0ef to your computer and use it in GitHub Desktop.
public class ContactWebService {
//GET: Retrieve data or resources from Salesforce.
@future(callout=true)
public static void getContacts() {
// Instantiate HTTP request and response objects
HttpRequest req = new HttpRequest();
HttpResponse response = new HttpResponse();
try {
// Set up HTTP request parameters
// SourceOrgNamedCredential is your SourceORG Named Credential
req.setEndpoint('callout:SourceOrgNamedCredential/services/apexrest/Contact/');
req.setHeader('Content-Type', 'application/json; charset=UTF-8');
req.setHeader('Accept', 'application/json');
req.setMethod('GET');
// Send HTTP request
Http http = new Http();
response = http.send(req);
// Check if response is successful
if (response.getStatusCode() == 200) {
// Log successful retrieval of contacts
System.debug('Contacts successfully retrieved.');
System.debug('Response Body: ' + response.getBody());
} else {
// Log failure to retrieve contacts
System.debug('Failed to retrieve contacts. Status code: ' + response.getStatusCode());
System.debug('Response Body: ' + response.getBody());
}
} catch (Exception e) {
// Log any exceptions that occur during the process
System.debug('Exception occurred while retrieving contacts: ' + e.getMessage());
}
}
//POST: Create new data or resources in Salesforce.
@future(callout=true)
public static void postContact() {
// Define the endpoint URL using a named credential
// SourceOrgNamedCredential is your SourceORG Named Credential
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:SourceOrgNamedCredential/services/apexrest/Contact/');
req.setMethod('POST');
// Set request headers
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
// Create the request body
Map<String, Object> requestBody = new Map<String, Object> {
'contactFirstName' => 'Shubham',
'contactLastName' => 'Vaishnav'
};
req.setBody(JSON.serialize(requestBody));
try {
// Send the request
Http http = new Http();
HttpResponse response = http.send(req);
// Check if the response is successful (status code 200)
if (response.getStatusCode() == 200) {
// Handle the response
system.debug('Contacts successfully created.');
system.debug('response body: ' + response.getBody());
} else {
// Handle other response
System.debug('response received: ' + response.getStatusCode() + ' - ' + response.getStatus());
}
} catch (Exception e) {
// Handle exception during HTTP callout
System.debug('Error sending contact to target org: ' + e.getMessage());
}
}
//PUT: Update existing data or resources in Salesforce.
@future(callout=true)
public static void putContact() {
HttpRequest req = new HttpRequest();
// SourceOrgNamedCredential is your SourceORG Named Credential
// 003F900001gMifVIAS is the Id of targetORG contact which we are updateing
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());
}
}
//PATCH: Partially update existing data or resources in Salesforce.
@future(callout=true)
public static void patchContact() {
HttpRequest req = new HttpRequest();
// SourceOrgNamedCredential is your SourceORG Named Credential
// 003F900001gMifVIAS is the Id of targetORG contact which we are updateing
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' => 'Updated Steven1',
'contactLastName' => 'Updated Foster2'
};
String requestBody = JSON.serialize(requestBodyMap);
req.setBody(requestBody);
req.setMethod('PATCH');
Http http = new Http();
HttpResponse response = http.send(req);
// Error handling
if (response.getStatusCode() == 200) {
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());
}
}
//DELETE: Remove data or resources from Salesforce.
@future (callout=true)
public static void deleteContacts () {
// Creating a new HTTP request
HttpRequest req = new HttpRequest();
// Setting the endpoint for the HTTP request
// SourceOrgNamedCredential is your SourceORG Named Credential
// 003F900001gMifVIAS is the Id of targetORG contact which we are updating
req.setEndpoint('callout:SourceOrgNamedCredential/services/apexrest/Contact/003F900001gMifVIAS');
// Setting headers for the HTTP request
req.setHeader('content-type','application/json; charset=UTF-8');
req.setHeader('Accept','application/json');
// Setting the HTTP method to DELETE
req.setMethod('DELETE');
// Creating a new HTTP object
Http http = new Http();
// Sending the HTTP request
HttpResponse response = http.send(req);
// Debugging the response
System.debug('response 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