function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SF7SF7 

Trigger to Update Manager Filed 4 users without Manage Users

I have added a custom lookup field on user object and if users update that field then it update Standard Manger field ( which is not accessible to them without Manage user permission) . And my trigger works great and the problem is when we need to deactivate the user it throws me a error saying the user is a manager for other users. and i need to go and remove them . But in Standard functionlity it doesnt happen that way. And if i try to remove the manager info from custom field then it even remove the info from standard field and i am ultimately losing all the information. Any suggestions please


trigger UserManagerUpdate on User(before insert, before update) {
  if (trigger.isInsert || trigger.isUpdate) {
    for (User myUser: Trigger.New) {
      myUser.ManagerId = myUser.Manager__c;
    }
  }
}

Thanks
Akhil
Best Answer chosen by SF7
SF7SF7
Hey Ramu,

Thanks for the reply 

I am getting error System.StringException: Invalid id: : Trigger.UserManagerUpdate: line 4, column 1

All Answers

RamuRamu (Salesforce Developers) 
Add a condition in your code as below

trigger UserManagerUpdate on User(before insert, before update) {
  if (trigger.isInsert || trigger.isUpdate) {
    for (User myUser: Trigger.New) {
      if(myUser.ManagerID==''){
myUser.ManagerId = myUser.Manager__c;
}
    }
  }
}
SF7SF7
Hey Ramu,

Thanks for the reply 

I am getting error System.StringException: Invalid id: : Trigger.UserManagerUpdate: line 4, column 1
This was selected as the best answer
RamuRamu (Salesforce Developers) 
Change this from if(myUser.ManagerID=='') to if(myUser.ManagerID==null) . Please let me know the results