ゲストユーザフローのカスタムコンポーネントの作成
コミュニティゲストユーザのフローで、代替ユーザ登録画面、複雑なディシジョンツリー、および条件付きフォームを提供し、ユーザ情報を収集できるようにします。次の例では、Site クラス API を使用します。詳細は、Salesforce の『Apex 開発者ガイド』の「Site クラス」を参照してください。
1.カスタム Lightning カスタムコンポーネントの作成
ログインまたはセルフ登録のゲストユーザフローを使用するには、lightning:availableForFlowScreens を実装するカスタムコンポーネントが必要です。
シンプルなデータコレクション設定フローのサンプルコードを次に示します。
1<aura:component implements="lightning:availableForFlowScreens" controller="CommunitySelfRegController">
2 <aura:attribute name="email" type="String" default=""/>
3 <aura:attribute name="fname" type="String" default=""/>
4 <aura:attribute name="lname" type="String" default=""/>
5 <aura:attribute name="starturl" type="String" default=""/>
6 <aura:attribute name="password" type="String" default=""/>
7 <aura:attribute name="hasOptedTracking" type="Boolean" default="false"/>
8 <aura:attribute name="hasOptedSolicit" type="Boolean" default="false"/>
9 <aura:attribute name="op_url" type="String" default="" description="login url after user is created. "/>
10
11 <aura:handler name="init" value="{!this}" action="{!c.init}" />
12
13 <aura:if isTrue="{! (empty(v.op_url))}">
14 <!-- empty url, the user is not yet created -->
15 <h3> Registering user. Please wait. </h3>
16
17 <aura:set attribute="else">
18 <!-- User created, show link to login -->
19 <h3> Success! Your account has been created. </h3>
20
21 <button class="slds-button slds-button_neutral" onclick="{!c.login}">Login</button>
22 </aura:set>
23 </aura:if>
24</aura:component>コントローラファイル:
1({
2 init : function(cmp) {
3 let email = cmp.get("v.email"),
4 fname = cmp.get("v.fname"),
5 lname = cmp.get("v.lname"),
6 pass = cmp.get("v.password"),
7 startUrl = cmp.get("v.starturl"),
8 hasOptedSolicit = cmp.get("v.hasOptedSolicit"),
9 hasOptedTracking = cmp.get("v.hasOptedTracking");
10
11 let action = cmp.get("c.createExternalUser");
12 action.setParams(
13 {
14 username: email,
15 password: pass,
16 startUrl: startUrl,
17 fname: fname,
18 lname: lname,
19 hasOptedTracking: hasOptedTracking,
20 hasOptedSolicit: hasOptedSolicit
21 });
22
23 action.setCallback(this, function(res) {
24 if (action.getState() === "SUCCESS") {
25 cmp.set("v.op_url", res.getReturnValue());
26 }
27 });
28 $A.enqueueAction(action);
29 },
30
31 login: function(cmp){
32 let url = cmp.get("v.op_url");
33 window.location.href = url;
34 }
35})デザインファイル:
1<design:component>
2 <design:attribute name="email" />
3 <design:attribute name="fname" />
4 <design:attribute name="lname" />
5 <design:attribute name="password" />
6 <design:attribute name="hasOptedTracking" />
7 <design:attribute name="hasOptedSolicit" />
8</design:component>2.Apex クラスの作成
次の例では、クラス CommunitySelfRegController を作成します。このクラスは新規コミュニティユーザを登録する Lightning コンポーネントで使用します。
1public class CommunitySelfRegController {
2 @AuraEnabled
3 public static String createExternalUser(
4 String username, String password, String startUrl, String fname,
5 String lname, Boolean hasOptedTracking, Boolean hasOptedSolicit) {
6 Savepoint sp = null;
7 try {
8 sp = Database.setsavepoint();
9 system.debug(sp);
10
11 // Creating a user object.
12 User u = new User();
13 u.Username = username;
14 u.Email = username;
15 u.FirstName = fname;
16 u.LastName = lname;
17
18 // Default UI for mobile is set to S1 for user created using site object.
19 // Enable this perm to change it to community.
20 u.UserPreferencesHideS1BrowserUI = true;
21
22 // Generating unique value for community nickname.
23 String nickname = ((fname != null && fname.length() > 0) ? fname.substring(0,1) : '' ) + lname.substring(0,1);
24 nickname += String.valueOf(Crypto.getRandomInteger()).substring(1,7);
25 u.CommunityNickname = nickname;
26
27 System.debug('creating user');
28
29 // Creating portal user.
30 // Passing in null account ID forces the system to read this from the network setting (set using Community Workspaces).
31 String userId = Site.createPortalUser(u, null, password);
32
33 // Setting consent selection values.
34 // For this, GDPR (Individual and Consent Management) needs to be enabled in the org.
35 Individual ind = new Individual();
36 ind.LastName = lname;
37 ind.HasOptedOutSolicit = !hasOptedSolicit;
38 ind.HasOptedOutTracking = !hasOptedTracking;
39 insert(ind);
40
41 // Other contact information can be updated here.
42 Contact contact = new Contact();
43 contact.Id = u.ContactId;
44 contact.IndividualId = ind.Id;
45 update(contact);
46
47 // return login url.
48 if (userId != null && password != null && password.length() > 1) {
49 ApexPages.PageReference lgn = Site.login(username, password, startUrl);
50 return lgn.getUrl();
51 }
52 }
53 catch (Exception ex) {
54 Database.rollback(sp);
55 System.debug(ex.getMessage());
56 return null;
57 }
58 return null;
59 }
60}
61Collapse
62
63}