function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RyanLorenzenRyanLorenzen 

testing connection to external system

Is there an easy to confirm that Salesforce has access to an external endpoint?   I want to do this before I do any Dev.  I need to simulate a call coming in from a Salesforce IP, not my own.
ClintLeeClintLee
Hi Ryan,

You can use the Http, HttpRequest, and HttpResponse classes.  

Open the Developer Console and run an anonymous script by clicking Debug > Open Execute Anonymous Window.

Execute the following:
 
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.yourendpoint.com');
req.setMethod('POST'); // you could also set this to 'GET' and remove the next line
req.setBody('body of the request');

HttpResponse response = http.send(req);
System.debug('BODY: ' + response.getBody());
System.debug('STATUS: ' + response.getStatusCode());
* Before doing this, you'll need to add your endpoint to the Remote Site Settings if you haven't already done so.  Go to Setup > Remote Site Settings > Add a new Endpoint.  Otherwise, Salesforce will not allow the callout to this endpoint.

Hope that helps,

Clint

 
RyanLorenzenRyanLorenzen
Thanks, but Im getting an error Line: 7, Column: 1 System.CalloutException: Read timed out This endpoint requires a login and password, which I have. I just need Salesforce to be the one logging in, so it’s their IP. Can that be added in to what you provided? Do I need to put anything for the Post, Body, and status? I don’t know what those are for. Thanks!
ClintLeeClintLee
If your endpoint allows BASIC authentication then try the following:
 
String username = 'your_username';
String password = 'your_password';

HTTP http = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setEndpoint('http://www.yourendpoint.com');
req.setMethod('GET');

Blob authValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(authValue);
req.setHeader('Authorization', authorizationHeader);

try {
    HTTPResponse response = http.send(req);
    System.debug('STATUS: ' + response.getStatusCode());
} catch(Exception e) {
    System.debug('Error: ' + e.getMessage());
}

 
ClintLeeClintLee
In the example above, you need to swap out your real username, password, and endpoint in the appropriate places.