Just like any other Apex code, a Salesforce Connect custom adapter can make callouts. If the connection to the external system requires authentication, incorporate the authentication parameters into the callout.
Authentication parameters are encapsulated in a ConnectionParams object and provided to your DataSource.Connection class’s constructor.
For example, if your connection requires an OAuth access token, use code similar to the following.
public HttpResponse getResponse(String url) { Http httpProtocol = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint(url); request.setMethod('GET'); request.setHeader('Authorization', 'Bearer ' + this.connectionInfo.oauthToken); HttpResponse response = httpProtocol.send(request); return response; }
If your connection necessitates basic password authentication, employ code resembling the following example.
public HttpResponse getResponse(String url) { Http httpProtocol = new Http(); HttpRequest request = new HttpRequest(); request.setEndPoint(url); request.setMethod('GET'); string encodedHeaderValue = EncodingUtil.base64Encode(Blob.valueOf( this.connectioninfo.username + ':' + this.connectionInfo.password)); request.setHeader('Authorization', 'Basic ' + encodedHeaderValue); HttpResponse response = httpProtocol.send(request); return response; }
Using Named Credentials as Callout Endpoints with Salesforce Connect Custom Adapters
A Salesforce Connect custom adapter retrieves the necessary credentials from Salesforce whenever they are required. Nevertheless, your Apex code is responsible for applying these credentials to all callouts, except for those that specify named credentials as the callout endpoints. Named credentials allow Salesforce to handle the authentication process, relieving your code from this burden.
If all the callouts made by your custom adapter use named credentials, you can configure the external data source’s Authentication Protocol field to “No Authentication.” The named credentials will automatically include the required certificates and can append standard authorization headers to the callouts. Furthermore, there’s no need to define a remote site for an Apex callout endpoint that is defined as a named credential.