3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Saleforce Siteクラスの使用例

Last updated at Posted at 2020-07-25

プロジェクトにSiteクラスの使用例を整理する
コミュニティユーザを利用時、Siteクラスを利用しログイン関連の各種処理が簡単に実現できる。

  • Site.login
login.cls
PageReference homePage;// 遷移先画面を格納
String mypageUrl = '/testpath';// mypageUrl相対パスを指定
homePage = Site.login(userId, password, mypageUrl);
  • Site.validatePassword
validatePassword(Visualforce).cls
User user = new User();
user.UserName  = 'testUserName';// UserName設定必須
Site.validatePassword(user, password, rePassword);
// 検証エラーの場合
// メソッドが Lightning コントローラで実行されたときに検証が失敗すると、失敗した検証を説明する Apex 例外が発生します。
// メソッドが Visualforce コントローラで実行されたときに検証が失敗すると、Visualforce エラーメッセージが提供されます。
// 以下の実装例はVisualforceの例です。
if (ApexPages.getMessages().size() > 0) {
    // エラー処理
}
  • Site.createExternalUser
createExternalUser.cls
User u = new User();
u.Username = userName;
u.Email = email;
u.FirstName = firstName;
u.LastName = lastName;
u.CommunityNickname = communityNickname;
u.ProfileId = profileId;

String accountId = '';
String userId;
try {
    userId = Site.createExternalUser(u, accountId, password);
} catch(Site.ExternalUserCreateException ex) {
    List<String> errors = ex.getDisplayMessages();
    for (String error : errors)  {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
    }
    // This message is used for debugging. Do not display this in the UI to the end user.
    // It has the information around why the user creation failed.
    System.debug(ex.getMessage());
}
  • Site.changePassword
changePassword.cls
Site.changePassword(newPassword, verifyNewPassword, oldpassword); 
  • Site.forgotPassword
forgotPassword.cls
boolean success = Site.forgotPassword(username);
PageReference pr = yourForgotPasswordConfirmPage;
pr.setRedirect(true);
if (success) return pr;
  • Site.isValidUsername
isValidUsername.cls
Site.forgotPassword(username);
ApexPages.PageReference checkEmailRef = new PageReference(checkEmailUrl);
if(!Site.isValidUsername(username)) {
    return Label.Site.invalid_email;
}
aura.redirect(checkEmailRef);
return null;

※ ボーナス

isGuestUser.cls
// ユーザであるかどうかの判定
Auth.CommunitiesUtil.isGuestUser();
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?