Site Class
Namespace
Site Methods
The following are methods for Site. All methods are static.
changePassword(newPassword, verifyNewPassword, oldPassword)
Signature
public static System.PageReference changePassword(String newPassword, String verifyNewPassword, String oldPassword)
Parameters
Return Value
Type: System.PageReference
createExternalUser(user, accountId)
Signature
public static Id createExternalUser(SObject user, String accountId)
Parameters
- user
- Type: SObject
- Information required to create a user.
The email address of the user is used to look for matching contacts associated with the specified accountId. If a matching contact is found and is already used by an external user, self-registration isn’t successful. If a matching contact is found but isn’t used by an external user, it is used for the new external user. If there is no matching contact, a new contact is created for the new external user.
- accountId
- Type: String
- The ID of the account you want to associate the user with.
Usage
This method throws Site.ExternalUserCreateException when user creation fails.
The nickname field is required for the User sObject when using the createExternalUser method.
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
createExternalUser(user, accountId, password)
Signature
public static Id createExternalUser(SObject user, String accountId, String password)
Parameters
- user
- Type: SObject
- Information required to create a user.
The email address of the user is used to look for matching contacts associated with the specified accountId. If a matching contact is found and is already used by an external user, self-registration isn’t successful. If a matching contact is found but isn’t used by an external user, it is used for the new external user. If there is no matching contact, a new contact is created for the new external user.
- accountId
- Type: String
- The ID of the account you want to associate the user with.
- password
- Type: String
- The password of the Salesforce Site or Experience Cloud site user. If not specified, or if set to null or an empty string, this method sends a new password email to the portal user.
Usage
This method throws Site.ExternalUserCreateException when user creation fails.
The nickname field is required for the User sObject when using the createExternalUser method.
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
createExternalUser(user, accountId, password, sendEmailConfirmation)
Signature
public static Id createExternalUser(SObject user, String accountId, String password, Boolean sendEmailConfirmation)
Parameters
- user
- Type: SObject
-
Information required to create a user.
The email address of the user is used to look for matching contacts associated with the specified accountId. If a matching contact is found and is already used by an external user, self-registration isn’t successful. If a matching contact is found but isn’t used by an external user, it is used for the new external user. If there is no matching contact, a new contact is created for the new external user.
- accountId
- Type: String
- The ID of the account you want to associate the user with.
- password
- Type: String
- The password of the Salesforce Site or Experience Cloud site user. If not specified, or if set to null or an empty string, this method sends a new password email to the portal user.
- sendEmailConfirmation
- Type: Boolean
- Determines whether a new user email is sent to the portal user. Set it to true to send a new user email to the portal user. The default is false, that is, the new user email isn't sent.
Usage
This method throws Site.ExternalUserCreateException when user creation fails.
The nickname field is required for the User sObject when using the createExternalUser method.
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
createPersonAccountPortalUser(user, ownerId, password)
Signature
public static ID createPersonAccountPortalUser(sObject user, String ownerId, String password)
Return Value
Type: ID
Usage
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
createPersonAccountPortalUser(user, ownerId, recordTypeId, password)
Signature
public static ID createPersonAccountPortalUser(sObject user, String ownerId, String recordTypeId, String password)
Return Value
Type: ID
Usage
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
createPortalUser(user, accountId, password, sendEmailConfirmation)
Signature
public static ID createPortalUser(sObject user, String accountId, String password, Boolean sendEmailConfirmation)
Parameters
- user
- Type: sObject
- accountId
- Type: String
- password
- Type: String
- (Optional) The password of the portal user. If not specified, or if set to null or an empty string, this method sends a new password email to the portal user.
- sendEmailConfirmation
- Type: Boolean
- (Optional) Determines whether a new user email is sent to the portal user. Set it to true to send a new user email to the portal user. The default is false, that is, the new user email isn't sent.
Return Value
Type: ID
Usage
If you’re using API version 34.0 or later, we recommend using the createExternalUser() methods because they offer better error handling than this method.
The nickname field is required for the user sObject when using the createPortalUser method.
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
forgotPassword(username, emailTemplateName)
Signature
public static Boolean forgotPassword(String username,String emailTemplateName)
Parameters
Return Value
Type: Boolean
Usage
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
Calls to this method are subject to rate-limiting. If your rate of calls exceeds the limit, Salesforce doesn't send the password reset email. If you experience this issue, try waiting for an hour before you send another call.
forgotPassword(username)
Signature
public static Boolean forgotPassword(String username)
Parameters
- username
- Type: String
Return Value
Type: Boolean
Usage
Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
Calls to this method are subject to rate-limiting. If your rate of calls exceeds the limit, Salesforce doesn't send the password reset email. If you experience this issue, try waiting for an hour before you send another call.
getAnalyticsTrackingCode()
Signature
public static String getAnalyticsTrackingCode()
Return Value
Type: String
getCurrentSiteUrl()
Signature
Note that this may return the referring page's URL instead of the current request's URL. The returned value includes the path prefix and always ends with a / character. If the current request is not a site request, then this method returns null. If the current request is not a site request, then this method returns null. This method was replaced by getBaseUrl in API version 30.0.
public static String getCurrentSiteUrl()
Return Value
Type: String
Usage
Use getBaseUrl() instead.
getBaseCustomUrl()
Signature
public static String getBaseCustomUrl()
Return Value
Type: String
Usage
This method replaces getCustomWebAddress() and includes the custom URL's path prefix.
getBaseInsecureUrl()
Signature
public static String getBaseInsecureUrl()
Return Value
Type: String
getBaseRequestUrl()
Signature
public static String getBaseRequestUrl()
Return Value
Type: String
getBaseSecureUrl()
Signature
public static String getBaseSecureUrl()
Return Value
Type: String
getBaseUrl()
Signature
public static String getBaseUrl()
Return Value
Type: String
Usage
This method replaces getCurrentSiteUrl().
getCustomWebAddress()
Signature
Returns the request's custom URL if it doesn't end in Lightning Platform or returns the site's primary custom URL. If neither exist, then this returns null. Note that the URL's path is always the root, even if the request's custom URL has a path prefix. If the current request is not a site request, then this method returns null. The returned value always ends with a / character.
public static String getCustomWebAddress()
Return Value
Type: String
Usage
Use getBaseCustomUrl() instead.
getDomain()
Signature
public static String getDomain()
Return Value
Type: String
getErrorDescription()
Signature
public static String getErrorDescription()
Return Value
Type: String
getErrorMessage()
Signature
public static String getErrorMessage()
Return Value
Type: String
getExperienceId()
Signature
public static String getExperienceId()
Return Value
Type: String
Usage
Use the getExperienceId and setExperienceId methods to implement dynamic login experiences. You can set the experience ID with setExperienceId or by extending the following endpoints with expid_value.
- community-url/services/oauth2/authorize/expid_value
- community-url/idp/endpoint/HttpPost/expid_value
- community-url/idp/endpoint/HttpRedirect/expid_value
- community-url_login_page/expid={value}
- community-url/CommunitiesSelfReg?expid={value}
- secur/forgotpassword.jsp?expid={value}
The cookie is set when the browser loads the URLs with the expid values.
getMasterLabel()
Signature
public static String getMasterLabel()
Return Value
Type: String
getName()
Signature
public static String getName()
Return Value
Type: String
getOriginalUrl()
Signature
public static String getOriginalUrl()
Return Value
Type: String
getPasswordPolicyStatement()
Signature
public static String getPasswordPolicyStatement()
Return Value
Type: String
getPathPrefix()
Signature
public static String getPathPrefix()
Return Value
Type: String
getPrefix()
Signature
Returns the URL path prefix of the current site. For example, if your site URL is MyDomainName.my.salesforce-sites.com/partners, /partners is the path prefix. Returns null if the prefix isn’t defined. If the current request is not a site request, then this method returns a null.
public static String getPrefix()
Return Value
Type: String
getSiteId()
Signature
public static String getSiteId()
Return Value
Type: Id
getTemplate()
Signature
public static System.PageReference getTemplate()
Return Value
Type: System.PageReference
getSiteType()
Signature
public static String getSiteType()
Return Value
Type: String
getSiteTypeLabel()
Signature
public static String getSiteTypeLabel()
Return Value
Type: String
isLoginEnabled()
Signature
public static Boolean isLoginEnabled()
Return Value
Type: Boolean
isPasswordExpired()
Signature
public static Boolean isPasswordExpired()
Return Value
Type: Boolean
isRegistrationEnabled()
Signature
public static Boolean isRegistrationEnabled()
Return Value
Type: Boolean
login(username, password, startUrl)
Signature
public static System.PageReference login(String username, String password, String startUrl)
Return Value
Type: System.PageReference
Usage
All DML statements before the call to Site.login get committed. It’s not possible to roll back to a save point that was created before a call to Site.login.
passwordlessLogin(userId, methods, startUrl)
Signature
public static System.PageReference passwordlessLogin(Id userId, List<Auth.VerificationMethod> methods, String startUrl)
Parameters
- userId
- Type: Id
- ID of the user to log in.
- methods
- Type: List<Auth.VerificationMethod>
- List of identity verification methods available to the user for passwordless login.
- startUrl
- Type: String
- Path to the page that users see after they log in.
Return Value
Type: System.PageReference
Usage
Include this method in the Apex controller of a custom login page implementation.
PasswordlessLogin Example
This simple code example of an Apex controller contains the passwordlessLogin method. The PageReference returned by passwordlessLogin redirects the user to the Salesforce Verify page. When the user enters the correct code, the user is redirected to the site page specified by the start URL.
global with sharing class MFILoginController
{
//Input variables
global String input {get; set;}
public String startURL {get; set;}
public List<Auth.VerificationMethod> methods;
public String error;
global MFILoginController()
{
// Add verification methods in priority order
methods = new List<Auth.VerificationMethod>();
methods.add(Auth.VerificationMethod.SMS);
methods.add(Auth.VerificationMethod.EMAIL);
methods.add(Auth.VerificationMethod.U2F);
methods.add(Auth.VerificationMethod.SALESFORCE_AUTHENTICATOR);
methods.add(Auth.VerificationMethod.TOTP);
}
global PageReference login() {
List<User> users = null;
// Empty input
if(input == null || input == '')
{
error = 'Enter Username';
return null;
}
users = [select name, id, email from User where username=:input];
if(users == null || users.isEmpty())
{
error = 'Can\'t find a user';
return null;
}
if (startURL == null) startURL = '/';
return Site.passwordlessLogin(users[0].id, methods, startURL);
}
}setExperienceId(expIdValue)
Signature
public static void setExperienceId(String expIdValue)
Parameters
- expIdValue
- Type: String
- A value that indicates the user’s login experience.
The value must contain alphanumeric characters only, up to 30 characters.
Usage
Use setExperienceId when you’re implementing dynamic login experiences. A login experience refers to a login page plus any secondary pages associated with the login page (such as multi-factor authentication (MFA) or a login flow). You define different login experiences depending on who users are or where they’re logging in from. For example, you can require a different registration process based on the user’s location. In this case, expIdValue includes a state or country code. When the user logs in, the URL contains the experience ID parameter, {expid}. The {expid} parameter is replaced by the value stored in expIdValue, such as .jp. Then the user is redirected to the Japanese login experience.
Example
String expid = ApexPages.currentPage().getParameters().get('expid');
if (expId != null) {
Site.setExperienceId(expId);
}setPortalUserAsAuthProvider(user, contactId)
Signature
public static Void setPortalUserAsAuthProvider(sObject user, String contactId)
Return Value
Type: Void
Usage
- This method is only valid when a site is associated with a Customer Portal.
- Calls to this method in API version 30.0 and later can’t commit the transaction automatically. Calls to this method before API version 30.0 commit the transaction, making it impossible to roll back to a save point before the call.
- For more information on an authentication provider, see RegistrationHandler.
validatePassword(user, password, confirmPassword)
Signature
public static void validatePassword(SObject user, String password, String confirmPassword)
Parameters
Return Value
Type: void