The following are methods for Site. All methods are static.
public static System.PageReference changePassword(String newPassword, String verifyNewPassword, String oldPassword)
Type: System.PageReference
public static Id createExternalUser(SObject user, String accountId)
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.
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.
public static Id createExternalUser(SObject user, String accountId, String password)
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.
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.
public static Id createExternalUser(SObject user, String accountId, String password, Boolean sendEmailConfirmation)
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.
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.
public static ID createPersonAccountPortalUser(sObject user, String ownerId, String password)
Type: ID
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.
public static ID createPersonAccountPortalUser(sObject user, String ownerId, String recordTypeId, String password)
Type: ID
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.
public static ID createPortalUser(sObject user, String accountId, String password, Boolean sendEmailConfirmation)
Type: ID
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.
public static Boolean forgotPassword(String username,String emailTemplateName)
Type: Boolean
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.
public static Boolean forgotPassword(String username)
Type: Boolean
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.
public static String getAnalyticsTrackingCode()
Type: String
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()
Type: String
Use getBaseUrl() instead.
public static String getBaseCustomUrl()
Type: String
This method replaces getCustomWebAddress() and includes the custom URL's path prefix.
public static String getBaseInsecureUrl()
Type: String
public static String getBaseRequestUrl()
Type: String
public static String getBaseSecureUrl()
Type: String
public static String getBaseUrl()
Type: String
This method replaces getCurrentSiteUrl().
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()
Type: String
Use getBaseCustomUrl() instead.
public static String getDomain()
Type: String
public static String getErrorDescription()
Type: String
public static String getErrorMessage()
Type: String
public static String getExperienceId()
Type: String
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.
The cookie is set when the browser loads the URLs with the expid values.
public static String getMasterLabel()
Type: String
public static String getName()
Type: String
public static String getOriginalUrl()
Type: String
public static String getPasswordPolicyStatement()
Type: String
public static String getPathPrefix()
Type: String
Returns the URL path prefix of the current site. For example, if your site URL is myco.force.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()
Type: String
public static String getSiteId()
Type: Id
public static System.PageReference getTemplate()
Type: System.PageReference
public static String getSiteType()
Type: String
public static String getSiteTypeLabel()
Type: String
public static Boolean isLoginEnabled()
Type: Boolean
public static Boolean isPasswordExpired()
Type: Boolean
public static Boolean isRegistrationEnabled()
Type: Boolean
public static System.PageReference login(String username, String password, String startUrl)
Type: System.PageReference
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.
public static System.PageReference passwordlessLogin(Id userId, List<Auth.VerificationMethod> methods, String startUrl)
Type: System.PageReference
Include this method in the Apex controller of a custom login page implementation.
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 community 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); } }
public static void setExperienceId(String expIdValue)
The value must contain alphanumeric characters only, up to 30 characters.
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 2FA authentication 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.
String expid = ApexPages.currentPage().getParameters().get('expid'); if (expId != null) { Site.setExperienceId(expId); }
public static Void setPortalUserAsAuthProvider(sObject user, String contactId)
Type: Void
public static void validatePassword(SObject user, String password, String confirmPassword)
Type: void