|
| 1 | +global class PrepareMySandbox implements SandboxPostCopy { |
| 2 | + global PrepareMySandbox() { |
| 3 | + // Implementations of SandboxPostCopy must have a no-arg constructor. |
| 4 | + // This constructor is used during the sandbox copy process. |
| 5 | + } |
| 6 | + |
| 7 | + global void runApexClass(SandboxContext context) { |
| 8 | + System.debug('Org ID: ' + context.organizationId()); |
| 9 | + System.debug('Sandbox ID: ' + context.sandboxId()); |
| 10 | + System.debug('Sandbox Name: ' + context.sandboxName()); |
| 11 | + |
| 12 | + updateProfilesAndResetPasswordsForPublicGroupMembers(); |
| 13 | + // Additional logic to prepare the sandbox for use can be added here. |
| 14 | + } |
| 15 | + |
| 16 | + public void updateProfilesAndResetPasswordsForPublicGroupMembers() { |
| 17 | + String publicGroupId = '00G5a000003ji0R'; |
| 18 | + String newProfileId = '00e0b000001KWuY'; |
| 19 | + |
| 20 | + Group publicGroup = getPublicGroup(publicGroupId); |
| 21 | + |
| 22 | + if (publicGroup != null) { |
| 23 | + List<User> usersToUpdate = getUsersToUpdate(publicGroup, newProfileId); |
| 24 | + |
| 25 | + if (!usersToUpdate.isEmpty()) { |
| 26 | + update usersToUpdate; |
| 27 | + System.debug('Profile updated for ' + usersToUpdate.size() + ' users.'); |
| 28 | + |
| 29 | + // Reset passwords for updated users |
| 30 | + resetPasswords(usersToUpdate); |
| 31 | + } else { |
| 32 | + System.debug('No eligible active users found in the Public Group.'); |
| 33 | + } |
| 34 | + } else { |
| 35 | + System.debug('Public Group not found.'); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private Group getPublicGroup(String groupId) { |
| 40 | + return [SELECT Id FROM Group WHERE Id = :groupId LIMIT 1]; |
| 41 | + } |
| 42 | + |
| 43 | + private List<User> getUsersToUpdate(Group publicGroup, String newProfileId) { |
| 44 | + List<User> usersToUpdate = new List<User>(); |
| 45 | + Set<Id> userIds = new Set<Id>(); |
| 46 | + |
| 47 | + // Get the current running User's Id |
| 48 | + Id currentUserId = UserInfo.getUserId(); |
| 49 | + |
| 50 | + for (GroupMember member : [SELECT UserOrGroupId FROM GroupMember WHERE GroupId = :publicGroup.Id]) { |
| 51 | + Id userOrGroupId = member.UserOrGroupId; |
| 52 | + if (userOrGroupId != null && userOrGroupId.getSObjectType() == User.SObjectType && userOrGroupId != currentUserId) { |
| 53 | + userIds.add(userOrGroupId); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Query and update active User profiles |
| 58 | + for (User user : [SELECT Id, ProfileId FROM User WHERE Id IN :userIds AND IsActive = true]) { |
| 59 | + user.ProfileId = newProfileId; |
| 60 | + usersToUpdate.add(user); |
| 61 | + } |
| 62 | + |
| 63 | + return usersToUpdate; |
| 64 | + } |
| 65 | + |
| 66 | + private void resetPasswords(List<User> users) { |
| 67 | + for (User u : users) { |
| 68 | + System.resetPassword(u.Id, true); // The second parameter generates a new password and sends an email |
| 69 | + } |
| 70 | + System.debug('Passwords reset for ' + users.size() + ' users.'); |
| 71 | + } |
| 72 | +} |
0 commit comments