· 7 years ago · Jan 27, 2019, 03:30 PM
1@future
2public static void createPortalUser(Set<Id> setAccounts) {
3
4 Set<String> setUCIAsFID = new Set<String>();
5 Map<String, Account> mapAccount = new Map<String, Account>();
6 Set<String> setExistingUser = new Set<String>();
7 Map<Id, Id> mapContacts = new Map<Id, Id>();
8
9 List<User> listPhysicianDashbrdUsr = new List<User>();
10
11 List<Account> accounts = [Select Id, PC_Roche_UCI_ID__c, Type, PatientConnect__PC_First_Name__c,
12 PatientConnect__PC_Last_Name__c, PatientConnect__PC_Email__c from Account where Id in :setAccounts];
13 for(Account newAcct : accounts) {
14 if (newAcct.PC_Roche_UCI_ID__c != null && newAcct.Type == 'Doctor') {
15 setUCIAsFID.add(newAcct.PC_Roche_UCI_ID__c);
16 mapAccount.put(newAcct.PC_Roche_UCI_ID__c, newAcct);
17 } // if (newAcct.PC_Roche_UCI_ID__c != null) {
18 } // for(Account newAcct : accounts) {
19
20 for (Contact c : [Select Id, AccountId from Contact where AccountId in :setAccounts]) {
21 mapContacts.put(c.AccountId, c.Id);
22 } // for (Contact c : [Select Id, AccountId fro
23
24 system.debug('## setUCIAsFID ## ' + setUCIAsFID);
25 if(!setUCIAsFID.isEmpty()) {
26
27 for (User u : [Select id from User where FederationIdentifier in :setUCIAsFID]) {
28 setExistingUser.add(u.FederationIdentifier);
29 }
30
31 Profile p = [select id from Profile where name = 'Physician Dashboard Login User'];
32 Map<String, Id> roleMap = new Map<String, Id>();
33
34 for(UserRole role: [select Id, DeveloperName from UserRole]){
35 roleMap.put(role.DeveloperName, role.Id);
36 }
37
38 //UserRole portalRole = [Select Id, name From UserRole where name = 'SSO AU Customer User'];
39
40 system.debug('## portalRole ## ' + p.id + ' ' + roleMap.get('SSOAUCustomerUser'));
41
42 //mapContacts = createPortalContact(setUCIAsFID, mapAccount);
43 system.debug('## mapContacts ## ' + mapContacts);
44 for (String fid : setUCIAsFID) {
45 system.debug('## fid ## ' + fid);
46 if (setExistingUser.contains(fid) == false) {
47
48 User u = new User(//alias = mapAccount.get(fid).PatientConnect__PC_First_Name__c.Substring(0,1) + mapAccount.get(fid).PatientConnect__PC_Last_Name__c.Substring(0,4),
49 alias = 'test123',
50 email=mapAccount.get(fid).PatientConnect__PC_Email__c,
51 emailencodingkey='UTF-8',
52 firstName=mapAccount.get(fid).PatientConnect__PC_First_Name__c,
53 lastname=mapAccount.get(fid).PatientConnect__PC_Last_Name__c,
54 languagelocalekey='en_US',
55 localesidkey='en_US',
56 profileid = p.id,
57 timezonesidkey = 'Australia/Brisbane',
58 FederationIdentifier = fid,
59 //UserType = 'CustomerSuccess',
60 contactId = mapContacts.get(mapAccount.get(fid).Id), //con.id,
61 //UserRoleId = roleMap.get('AU_Physician'),
62 PC_Roche_Affiliate__c = PC_Roche_Constants.AFFILIATE_AU,
63 username= System.now().millisecond() + fid +'@gmail.com');
64 listPhysicianDashbrdUsr.add(u);
65
66 } // if (setExistingUser.contains(fid) == false) {
67 } // for (String fid : setUCIAsFID) {
68
69 system.debug('## listPhysicianDashbrdUsr ## ' + listPhysicianDashbrdUsr);
70 if(!listPhysicianDashbrdUsr.isEmpty()) {
71 Database.insert(listPhysicianDashbrdUsr);
72 } // if(!listPhysicianDashbrdUsr.isEmpty()) {
73
74 }
75}