This document discusses various ways to integrate Salesforce with external systems including inbound and outbound integrations using REST, SOAP, and Salesforce to Salesforce. It provides steps to set up remote site settings to allow callouts to external URLs and examples of Apex code to make GET and POST calls to an external Heroku endpoint. It also covers exposing Apex classes through REST APIs or SOAP web services and testing callouts using mock callouts.
2. Create remote site settings first to allow the url to be accessible in salesforce.
Outbound Integration: How to invoke callouts
3. 1. From Setup, enter Remote Site Settings in the Quick Find box,
2. Click Remote Site Settings.
3. Click New Remote Site.
4. Provide a name for the remote site
5. Provide the URL for the remote site. This URL authorizes all subfolders for the endpoint, like
https://and https://th-apex-http-callout.herokuapp.com/path2. th-apex-http-
callout.herokuapp.com/path1
6. Provide a description for the remote site / service.
7. Click Save
To authorize remote callouts
4.
public static HttpResponse makeGetCallout() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for (Object animal: animals) {
System.debug(animal);
}
}
return response;
}
Get the data using callouts using heroku endpoint
5. public static HttpResponse makePostCallout() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody('{"name":"mighty moose"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
return response;
}
Post the data using callouts
6. Inbound Integration:How to expose Apex classes
SOAP :Protocol Neutral – accessible via HTTP, SMTP, TCP and other
application level protocols. Only XML. Requires WSDL
REST: REStricted to http protocol,but can use JSON, XML, Atom and
others.
12. package sfdc_rest;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
public class Main {
static final String USERNAME = "narenrr@yahoo.com";
static final String PASSWORD = "2WHVAUUcVWbxXMmKFQDEl1sgK0";
static final String LOGINURL = "https://login.salesforce.com";
static final String GRANTSERVICE =
"/services/oauth2/token?grant_type=password";
Advanced example – JAVA rest api
13. Generate enterprise WSDL
Generate Class wsdl
Enter user name /pwd/token
Get session id and url prefix
Invoke method with session id and correct endpoint url
How to expose webservice with SOAP
15. Between two prod orgs
One will be sender,other will be reciever of changes
Cannot be done with developer org.
Salesforce to Salesforce
16. Bad news salesforce does not allow callouts to be tested.
Good News: Mock callouts to the rescue. Basically simulates a static
resource as target ,instead of the actual url.
Purpose is to get code coverage of callouts
Testing callouts