Skip to content

Instantly share code, notes, and snippets.

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