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
masthan khanmasthan khan 

how to find dynamic manager for a user

Hai,
        I am trying to find the manager and managers.Manager when ever a record is inserted using trigger but it throwing error variable does not exist.

Code
public class Opportunity_handler {
    public string opname{set;get;}
    public static void afterInsert(List<Opportunity> optylist){
        for(opportunity a:optylist){
            string opname;
            opname=a.name;
        }
         opportunity op=[select id,ownerid from opportunity where name=:opname];
        user ow=[select id,manager.name,manager.id from user where  id=:op.ownerid];
         user mg1=[select id,manager.id from user where   id=:ow.manager.id];
           user vp1=[select id,manager.id from user where id=:mg1.manager.id];    
 }

}

Error:  Variable opname doesnot exist
Steven NsubugaSteven Nsubuga
The problem is that you are accessing opname in a static method.

I suggest you change your code by EITHER removing the static keyword from the method
public class Opportunity_handler {
    public string opname{set;get;}
    public void afterInsert(List<Opportunity> optylist){
        for(opportunity a:optylist){
            string opname;
            opname=a.name;
        }
         opportunity op=[select id,ownerid from opportunity where name=:opname];
        user ow=[select id,manager.name,manager.id from user where  id=:op.ownerid];
         user mg1=[select id,manager.id from user where   id=:ow.manager.id];
           user vp1=[select id,manager.id from user where id=:mg1.manager.id];    
 }

}

OR
adding the static keyword to the definition of  opname
public class Opportunity_handler {
    public static string opname{set;get;}
    public static void afterInsert(List<Opportunity> optylist){
        for(opportunity a:optylist){
            string opname;
            opname=a.name;
        }
         opportunity op=[select id,ownerid from opportunity where name=:opname];
        user ow=[select id,manager.name,manager.id from user where  id=:op.ownerid];
         user mg1=[select id,manager.id from user where   id=:ow.manager.id];
           user vp1=[select id,manager.id from user where id=:mg1.manager.id];    
 }

}