• RAJAGOPAL98
  • NEWBIE
  • 0 Points
  • Member since 2016
  • CONSULTANT
  • CAP

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 1
    Questions
  • 2
    Replies
plz help me with this.
1) I am having an User (let’s say Stephan). Who has a custom Field called : Immediate Manager. (Hierarchy Field)
2) We have an Account (let’s Say : Google). Whose owner is Stephan.
3) Whenever I will deactivate that user, then that User’s Immediate manager will become owner of all Account, which Stephan owned. Code with all best practices. (Bulkifying).
Also write Test method for this , which should cover at least 85%. functionality should be checked if working or not in test Class also.

trigger RecordOwnerChangeEx on User (after update) {   
 list<user> u=[select id, isActive, ManagerId from user where id in:trigger.new];
  // list<account> acc=[select id, ownerId from Account where ownerId in: ids ];
    list<account> ac=new list<account>();
    for(User uu:u){
        if(uu.IsActive == false && uu.ManagerId != null){
        for(account a:[select id, ownerId from Account where ownerId =:uu.id ]){
            a.ownerId = uu.ManagerId;
            ac.add(a);
        }
    }
    }
    update ac;

Tried with this but not working.