NamedCredentialAdapter
This interface allows your Adapter to be given a NamedCredential to use when making callouts.
For more information about using NamedCredentials for callouts, check out the Salesforce documentation. This interface has one method, where you are passed the string api name for the NamedCredential that has been selected for use by the Valence user. You can use this name directly in your callout URL.
The setNamedCredential() method is called, when needed, before any other methods your Adapter is implementing from other interfaces, such as planFetch() or fetchRecords().
The when needed means that the method will be called before say, a getTables() call if your Adapter registration record is marked as RequiresNamedCredentialForSchema__c. However, if RequiresNamedCredentialForSchema__c is unchecked, setNamedCredential() will not be called before getTables(). Basically, setNamedCredential() is contextually-sensitive and will only be called when it is needed.
Definition
/**
* Implement this interface if your Adapter needs a NamedCredential in order to communicate with its data source. See
* the Adapter custom metadata type for additional configuration options around when the NamedCredential comes into effect.
*/
global interface NamedCredentialAdapter extends Adapter {
/**
* Gives you the NamedCredential name that you will need in order to do an Apex callout or get information about
* the endpoint the User would like to talk to using your adapter.
*
* @param namedCredentialName The API name of a NamedCredential defined in this Salesforce org
*
* @see https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm
*/
void setNamedCredential(String namedCredentialName);
}
Example Usage
private String namedCredentialName = null;
public void setNamedCredential(String namedCredentialName) {
this.namedCredentialName = namedCredentialName;
}
public List<valence.RecordInFlight> fetchRecords(valence.LinkContext context, Object scope) {
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('callout:' + namedCredentialName);
req.setHeader('Content-Type', 'text/xml');
HttpResponse resp = new Http().send(req);
// process response and return RecordInFlight instances
}