Skip to content

Instantly share code, notes, and snippets.

@omniphx
Created March 10, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omniphx/3ca397e680e0a97679eb to your computer and use it in GitHub Desktop.
Save omniphx/3ca397e680e0a97679eb to your computer and use it in GitHub Desktop.
Remove User from hierachy fields
Id userId = '005i000000XXXXX';
List<String> fields = new List<String>{'Sales_Director__c','Account_Coordinator__c','Account_Services_Representative__c','Program_Director__c','Sales_Director__c','Placement_Manager__c','Inside_Sales_Representative__c','New_Family_Placement_Manager__c'};
User[] users = [
SELECT Id, Name, Account_Coordinator__c, Account_Services_Representative__c,Inside_Sales_Representative__c, New_Family_Placement_Manager__c,Placement_Manager__c, Program_Director__c, Sales_Director__c
FROM User
WHERE Account_Coordinator__c = :userId
OR Account_Services_Representative__c = :userId
OR Inside_Sales_Representative__c = :userId
OR New_Family_Placement_Manager__c = :userId
OR Placement_Manager__c = :userId
OR Program_Director__c = :userId
OR Sales_Director__c = :userId];
List<User> updateUsersList = new List<User>();
for(User user : users){
if(user.Account_Coordinator__c == userId){
user.Account_Coordinator__c = null;
}
if(user.Account_Services_Representative__c == userId){
user.Account_Services_Representative__c = null;
}
if(user.Inside_Sales_Representative__c == userId){
user.Inside_Sales_Representative__c = null;
}
if(user.New_Family_Placement_Manager__c == userId){
user.New_Family_Placement_Manager__c = null;
}
if(user.Placement_Manager__c == userId){
user.Placement_Manager__c = null;
}
if(user.Program_Director__c == userId){
user.Program_Director__c = null;
}
if(user.Sales_Director__c == userId){
user.Sales_Director__c = null;
}
//system.debug('user: ' + user.Name);
updateUsersList.add(user);
}
system.debug('updateUsersList: ' + updateUsersList);
update updateUsersList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment