Newer Version Available

This content describes an older version of this product. View Latest

NamedCredentials Class

Create, refresh, get, delete, replace, and update credentials. Create and get external credentials. Create and get named credentials. Get the URL for the OAuth token flow for an external credential.

Managed packages can access only the named credentials and external credentials that are included in or created from the package’s Apex code. If a managed package tries to access non-packaged named credentials and external credentials that a Salesforce admin created in the org, an error occurs.

Note

Namespace

ConnectApi

NamedCredentials Methods

The following are methods for NamedCredentials. All methods are static.

createCredential(requestBody)

Create a credential.

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.Credential createCredential(ConnectApi.CredentialInput requestBody)

Parameters

requestBody
Type: ConnectApi.CredentialInput
A ConnectApi.CredentialInput class.

Return Value

Type: ConnectApi.Credential

createCredential(requestBody, action)

Refresh an OAuth or AWS Roles Anywhere credential.

API Version

58.0

Requires Chatter

No

Signature

public static ConnectApi.Credential createCredential(ConnectApi.CredentialInput requestBody, ConnectApi.CreateCredentialAction action)

Parameters

requestBody
Type: ConnectApi.CredentialInput
A ConnectApi.CredentialInput class.
action
Type: ConnectApi.CreateCredentialAction
Action to take when creating the credential. Value is:
  • Refresh

Return Value

Type: ConnectApi.Credential

createExternalCredential(requestBody)

Create an external credential.

API Version

58.0

Requires Chatter

No

Signature

public static ConnectApi.ExternalCredential createExternalCredential(ConnectApi.ExternalCredentialInput requestBody)

Parameters

requestBody
Type: ConnectApi.ExternalCredentialInput
Input used to create or update an external credential.

Return Value

Type: ConnectApi.ExternalCredential

createNamedCredential(requestBody)

Create a named credential.

API Version

58.0

Requires Chatter

No

Signature

public static ConnectApi.NamedCredential createNamedCredential(ConnectApi.NamedCredentialInput requestBody)

Parameters

requestBody
Type: ConnectApi.NamedCredentialInput
Input used to create or update a named credential.

Return Value

Type: ConnectApi.NamedCredential

deleteCredential(externalCredential, principalName, principalType)

Delete a credential.

API Version

56.0

Requires Chatter

No

Signature

public static Void deleteCredential(String externalCredential, String principalName, ConnectApi.CredentialPrincipalType principalType)

Parameters

externalCredential
Type: String
Fully qualified developer name of the external credential.
principalName
Type: String
Name of the external credential named principal.
principalType
Type: ConnectApi.CredentialPrincipalType
Type of credential principal. Values are:
  • AwsStsPrincipal
  • NamedPrincipal
  • PerUserPrincipal

Return Value

Type: Void

deleteCredential(externalCredential, principalName, principalType, authenticationParameters)

Delete a credential with authentication parameters.

API Version

59.0

Requires Chatter

No

Signature

public static Void deleteCredential(String externalCredential, String principalName, ConnectApi.CredentialPrincipalType principalType, List<String> authenticationParameters)

Parameters

externalCredential
Type: String
Fully qualified developer name of the external credential.
principalName
Type: String
Name of the external credential named principal.
principalType
Type: ConnectApi.CredentialPrincipalType
Type of credential principal. Values are:
  • AwsStsPrincipal
  • NamedPrincipal
  • PerUserPrincipal
authenticationParameters
Type: List<String>
List of authentication parameters only for custom protocols, for example myApiKey,myApiSecret. If unspecified, all credentials are deleted.

Return Value

Type: Void

getCredential(externalCredential, principalName, principalType)

Get a credential.

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.Credential getCredential(String externalCredential, String principalName, ConnectApi.CredentialPrincipalType principalType)

Parameters

externalCredential
Type: String
Fully qualified developer name of the external credential.
principalName
Type: String
Name of the external credential named principal.
principalType
Type: ConnectApi.CredentialPrincipalType
Type of credential principal. Values are:
  • AwsStsPrincipal
  • NamedPrincipal
  • PerUserPrincipal

Return Value

Type: ConnectApi.Credential

getExternalCredential(developerName)

Get an external credential, including the named credentials and principals associated with it and the type and status of each principal.

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.ExternalCredential getExternalCredential(String developerName)

Parameters

developerName
Type: String
Fully qualified developer name of the external credential.

Return Value

Type: ConnectApi.ExternalCredential

getExternalCredentials()

Get external credentials that the user can authenticate to.

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.ExternalCredentialList getExternalCredentials()

getNamedCredential(developerName)

Get a named credential.

API Version

59.0

Requires Chatter

No

Signature

public static ConnectApi.NamedCredential getNamedCredential(String developerName)

Parameters

developerName
Type: String
Fully qualified developer name of the named credential.

Return Value

Type: ConnectApi.NamedCredential

getNamedCredentials()

Get a list of named credentials for the org.

API Version

59.0

Requires Chatter

No

Signature

public static ConnectApi.NamedCredentialList getNamedCredentials()

Return Value

Type: ConnectApi.NamedCredentialList

getOAuthCredentialAuthUrl(requestBody)

Get the URL for the OAuth token flow for an external credential.

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.OAuthCredentialAuthUrl getOAuthCredentialAuthUrl(ConnectApi.OAuthCredentialAuthUrlInput requestBody)

Parameters

requestBody
Type: ConnectApi.OAuthCredentialAuthUrlInput
A ConnectApi.OAuthCredentialAuthUrlInput class indicating the OAuth authentication flow.

Usage

Accepts input parameters representing a specific external credential and, optionally, a named principal. Returns the URL a user must visit to begin the authentication flow, ultimately returning authentication tokens to Salesforce. Use this method as part of building a customized or branded user interface to help users initiate authentication.

Example

1ConnectApi.OAuthCredentialAuthUrlInput input = new ConnectApi.OAuthCredentialAuthUrlInput();
2
3input.externalCredential = 'MyExternalCredentialDeveloperName';
4input.principalType = ConnectApi.CredentialPrincipalType.PerUserPrincipal;
5input.principalName = 'MyPrincipal'; // Only required when principalType = NamedPrincipal
6
7ConnectApi.OAuthCredentialAuthUrl output = ConnectApi.NamedCredentials.getOAuthCredentialAuthUrl(input);
8
9String authenticationUrl = output.authenticationUrl; // Redirect users to this URL to authenticate in the browser

patchCredential(requestBody)

Update custom credentials.

This method updates custom credentials. To replace a credential, use updateCredential(requestBody).

API Version

59.0

Requires Chatter

No

Signature

public static ConnectApi.Credential patchCredential(ConnectApi.CredentialInput requestBody)

Parameters

requestBody
Type: ConnectApi.CredentialInput
A ConnectApi.CredentialInput class. Only the custom credentials in the input class are updated.

Return Value

Type: ConnectApi.Credential

updateCredential(requestBody)

Replace a credential.

This method replaces a credential. To update a credential, use patchCredential(requestBody).

API Version

56.0

Requires Chatter

No

Signature

public static ConnectApi.Credential updateCredential(ConnectApi.CredentialInput requestBody)

Parameters

requestBody
Type: ConnectApi.CredentialInput
A ConnectApi.CredentialInput class.

Return Value

Type: ConnectApi.Credential