Created
January 8, 2018 19:57
-
-
Save mchang-r7/6218aaf656bdf5134b549271a1a8d768 to your computer and use it in GitHub Desktop.
Reassign users from old profile to new profile with anonymous Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class ReassignProfileJob { | |
private String oldProfileName; | |
private String newProfileName; | |
public ReassignProfileJob(String oldProfileName, String newProfileName) { | |
this.oldProfileName = oldProfileName; | |
this.newProfileName = newProfileName; | |
} | |
public void execute() { | |
List<User> affectedUsers = [ | |
SELECT Id | |
FROM User | |
WHERE ProfileId = :this.getProfileId(this.oldProfileName) | |
]; | |
Id newProfileId = this.getProfileId(this.newProfileName); | |
for (User eachUser : affectedUsers) { | |
eachUser.ProfileId = newProfileId; | |
} | |
update affectedUsers; | |
} | |
public Id getProfileId(String name) { | |
return [ | |
SELECT Id | |
FROM Profile | |
WHERE Name = :name | |
].Id; | |
} | |
} | |
String oldProfileName = 'Old Profile Name'; | |
String newProfileName = 'New Profile Name'; | |
new ReassignProfileJob(oldProfileName, newProfileName).execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment