Skip to content

Instantly share code, notes, and snippets.

@mchang-r7
Created January 8, 2018 19:57
Show Gist options
  • Save mchang-r7/6218aaf656bdf5134b549271a1a8d768 to your computer and use it in GitHub Desktop.
Save mchang-r7/6218aaf656bdf5134b549271a1a8d768 to your computer and use it in GitHub Desktop.
Reassign users from old profile to new profile with anonymous Apex
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