+ Start a Discussion
Adrian Camua 21Adrian Camua 21 

Test Connection to External System

Hi,

Is there an easy way to confirm that my Salesforce org has access to an external endpoint? Just needing to simulate a call coming in from a Salesforce IP.

I have added the endpoint to the remote site settings also, and I've tried executing the below
 
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());
(replacing the placeholder endpoint url in the code with that of the external system - same one in the remote site setting)

However I'm getting an error message:

System.CalloutException: Read timed out

Any ideas?
Adrian Camua 21Adrian Camua 21
Additionally... if I have a token which may need to be put in the code, could someone help me out with this? Thanks.
GovindarajGovindaraj
Hi Adrian,
1. If authentication enabled, First we need to hit the 'Authentication Server' and need to get the access token.
2. Then , By using that access token we need to hit the 'Resource Server' to get the information.

Consider we gonna integrate Salesforce with Salesforce. Below sample method will help you,
// To get accesstoken
Public void returnAccessToken()
    {
        string clientId = 'xxxxxxxxxxxxxxxxx';
        string clientSecret = 'xxxxxxxxxx';
        string username= 'xxxxxxxxxxx';
        string password='xxxxxxxxxxx';

        string reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

        HttpRequest req1 = new HttpRequest();
        Http h1 = new Http();
        req1.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        req1.setMethod('POST');
        req1.setBody(reqbody);
        
        HttpResponse res1 = h1.send(req1);
        
        Map<string,string> mapResponse1 = (Map<string,string>)JSON.deserialize(res1.getbody(),Map<string,string>.class);
        accessToken = mapResponse1.get('access_token');       
    }
    
// to get information 
   Public void makeCallout()
    {
        HttpRequest req2 = new HttpRequest();
        Http h2 = new Http();
        req2.setEndpoint('https://login.salesforce.com/services/apexrest/Acc');
        req2.setMethod('GET'); 
        req2.setHeader('Authorization','Bearer' +accessToken);
        
        HttpResponse res2 = h2.send(req2);
        
        accDetails mapResponse2 = (accDetails)JSON.deserialize(res2.getbody(),accDetails.class);
        accountName = mapResponse2.Name;        
    }

Pleas let us know, if this helps.

Thanks,
Govindaraj.S