How to access named credentials salesforce data in apex?

Is there a way of accessing the data (exact fields) stored in Spring 15' Named Credentials from apex? For example the username, password and endpoint link fields? And I don't mean for the callout, but for other purposes.

Answered by Clare Matthews

You can use Named Credentials salesforce to create an Endpoint URL with necessary parameters. For example, if this is is your URL:

https://www.someendpointurl.com?param1=named¶m2=credentials
In Apex, You can access this URL like this:
request.setEndpoint('callout:EndPointURL');
Where, EndPointURL is the Name of the Named Credentials.
Benefits of using Named Credentials:
Authentication is done by Salesforce and you need not to worry about that.
No need to create a Remote Site Setting if using a Named Credential.
Callout is easier to maintain. No hard coding involved.
If you are also using Sandboxes for callout, just create the Named Credentials with the same name and save different URLs.
Hope this helps..!!
But actually accessing the data stored in the Named Credentials fields (e.g. as String) is not possible.

Your Answer

Interviews

Parent Categories