+ Start a Discussion
Neeraj Sharma 103Neeraj Sharma 103 

I want to change account owner based on custom state field (Picklist) and account owner change based on role hierarchy and in role hierarchy the role will by state wise


Hi Everyone Please help me out from this scenario

 I want to update Account record owner incase custom state field is anyone selected from picklist(Gujarat,Rajasthan,Bihar,etc) with higher role and role checked 

User-added image
four levels above in role hiearchy if state chandigarh is select then (SOCG (SalesOfficer Chandigarh)) in this role the user will be updated on record owner and if no user in role of (SOCG) then above him(ASM CG) user is update on record owner this level go up to last level at (RM CG) 
and if RMCG in this role there is no user then by default  record owner will be update to this role user(RM CG)
 
Best Answer chosen by Neeraj Sharma 103
Sujeet PatelSujeet Patel
Hii 
Neeraj Sharma
I didn't run my code in my org due to your more amount of role but i try to resolve your issue.
My be It will help full for you.
My basic flow of code is below given
I hope this will help full for you
String role;
String owner;
List<Account> changeOwner=new List<Account>();
for(Account ac:trigger.new){
		if(ac.State=='Chandigarh'){
			role='SO CG';
			changeOwner.add(ac);
		}
}

UserRole u=[select id,parentID,name from UserRole where Name=:'role' limit 1];
User us=[select id from User where UserRoleId=u.id];
if(us!=null){
	owner=us.id;
}
else if(us==null){
	us=[select id from User where UserRoleId=u.parentID];
	owner=us.id;
	if(us==null){
		us=[select id,name from User where UserRole.Name='RM CG'];
		owner=us.id;
	}
}
for(Account ac:changeOwner){
	if(us!=null){
		ac.OwnerID=owner;
		}
}

 

All Answers

Sujeet PatelSujeet Patel
Hii 
Neeraj Sharma
I didn't run my code in my org due to your more amount of role but i try to resolve your issue.
My be It will help full for you.
My basic flow of code is below given
I hope this will help full for you
String role;
String owner;
List<Account> changeOwner=new List<Account>();
for(Account ac:trigger.new){
		if(ac.State=='Chandigarh'){
			role='SO CG';
			changeOwner.add(ac);
		}
}

UserRole u=[select id,parentID,name from UserRole where Name=:'role' limit 1];
User us=[select id from User where UserRoleId=u.id];
if(us!=null){
	owner=us.id;
}
else if(us==null){
	us=[select id from User where UserRoleId=u.parentID];
	owner=us.id;
	if(us==null){
		us=[select id,name from User where UserRole.Name='RM CG'];
		owner=us.id;
	}
}
for(Account ac:changeOwner){
	if(us!=null){
		ac.OwnerID=owner;
		}
}

 
This was selected as the best answer
Neeraj Sharma 103Neeraj Sharma 103
Hii
Sujeet Patel
thanks for reply 
UserRole u=[select id,parentID,name from UserRole where Name=:'role' limit 1];
User us=[select id from User where UserRoleId=u.id];
Above both query does not work its gives error Unexpected token'us' and as all
 
Sujeet PatelSujeet Patel
Hii
Neeraj
Its my mistake.
you can use either this one
UserRole u=[select id,parentID,name from UserRole where Name=:role limit 1];

or this one
UserRole u=[select id,parentID,name from UserRole where Name=:'SO CG' limit 1];

And i already told  you that i am not writting entire code.i am just writting similer code that will help to resolve your querry.
You should also modify this code according to our requirement.
I hope my this answer will help full for you.
 
Neeraj Sharma 103Neeraj Sharma 103
Thanks 
Sujeet Patel your code is useful for me thanks a lot i set my requirement according to your code
 
Neeraj Sharma 103Neeraj Sharma 103
Hi  @Sujeet Patel this is my below trigger code can you please help me i want to reduce lines of code in trigger my trigger is so long around 400 lines please help me

 
trigger Distributor on Account (before insert,before update) {
    
String role;
String owner;
    
  list<Account> changeOwner=new list<Account>();
    
    for(Account acc:trigger.new){
        
       if(acc.Under__c=='Chhattisgarh'){
           role='CG';
           changeOwner.add(acc);
       }
      
       else if(acc.Under__c=='Bihar'){
           role='BI';
          changeOwner.add(acc);
       }  
        
      else if(acc.Under__c=='Rajasthan') {
           role='RJ';
           changeOwner.add(acc);
           }  
      else if(acc.Under__c=='Gujarat'){
           role='GJ';
           changeOwner.add(acc);
           }
        
      else if(acc.Under__c=='Haryana'){
           role='HR';
           changeOwner.add(acc);
           
       }
        
      else if(acc.Under__c=='Jharkhand'){
           role='JH';
           changeOwner.add(acc);
       } 
        
      else if(acc.Under__c=='Maharashtra'){
           role='MH';
           changeOwner.add(acc);
       }
        
       else if(acc.Under__c=='Madhya Pradesh'){
           role='MP';
           changeOwner.add(acc);
       }  
       else if(acc.Under__c=='Uttar Pradesh'){
           role='UP';
           changeOwner.add(acc);
       } 
        
   }


  list <User> socg1=[select id,Name from User where UserRole.Name='SO'+role Order BY FirstName,LastName];
  list <User> socg2=[select id,Name from User where UserRole.Name='ASM ' Order BY FirstName,LastName];
  list <User> socg3=[select id,Name from User where UserRole.Name='DRM ' Order BY FirstName,LastName];
  list <User> socg4=[select id,Name from User where UserRole.Name='RM ' Order BY FirstName,LastName];
  
   if(socg1.size()>0){
        owner=socg1.get(0).id;
    }
    
     else if(socg1.size()<=0){
     
        if(socg2.size()>0){
            owner=socg2.get(0).id; 
        }
        
        else if(socg2.size()<=0){
            
            if(socg3.size()>0){
              owner=socg2.get(0).id;  
             }
        
        else if(socg3.size()<=0){
            
             if(socg4.size()>0) {
               owner=socg4.get(0).id;   
             }   
        }
      }
   }
   for(Account acc:changeOwner){
       
        if(socg1!=null){
            acc.OwnerId=Owner;
        }
    }
    
     list <User> sobi1=[select id,Name from User where UserRole.Name='SO BI' Order BY FirstName,LastName];
     list <User> sobi2=[select id,Name from User where UserRole.Name='ASM BI' Order BY FirstName,LastName];
     list <User> sobi3=[select id,Name from User where UserRole.Name='DRM BI' Order BY FirstName,LastName];
     list <User> sobi4=[select id,Name from User where UserRole.Name='RM BI' Order BY FirstName,LastName];
    
    
    if(sobi1.size()>0){
       owner=sobi1.get(0).id;
    }
   
    else if(sobi1.size()<=0){
     
        if(sobi2.size()>0){
            owner=sobi2.get(0).id; 
        }
   
       else if(sobi2.size()<=0){

            if(sobi3.size()>0){
              owner=sobi2.get(0).id;  
             }
    
        else if(sobi3.size()<=0){

             if(sobi4.size()>0){
               owner=sobi4.get(0).id;   
             } 
        }
      }
    }
    
    for(Account acc:changeOwner){ 
       
        if(sobi1!=null){
            acc.OwnerId=Owner;
        }
    }
    

     list <User> sorj1=[select id,Name from User where UserRole.Name='SO RJ' Order BY FirstName,LastName];
     list <User> sorj2=[select id,Name from User where UserRole.Name='ASM RJ' Order BY FirstName,LastName];
     list <User> sorj3=[select id,Name from User where UserRole.Name='DRM RJ' Order BY FirstName,LastName];
     list <User> sorj4=[select id,Name from User where UserRole.Name='RM RJ' Order BY FirstName,LastName];
    
    
    if(sorj1.size()>0){
       owner=sorj1.get(0).id;
    }
   
     else if(sorj1.size()<=0){
     
        if(sorj2.size()>0){
            owner=sorj2.get(0).id; 
        }
        
        else if(sorj2.size()<=0){

            if(sorj3.size()>0){
              owner=sorj2.get(0).id;  
             }
        
        else if(sorj3.size()<=0){

             if(sorj4.size()>0){
               owner=sorj4.get(0).id;   
             }   
        }    
      }      
    } 
    
  for(Account acc:changeOwner){

        if(sorj1!=null){
            acc.OwnerId=Owner;
        }
    }
      
     list <User> sogj1=[select id,Name from User where UserRole.Name='SO GJ' Order BY FirstName,LastName];
     list <User> sogj2=[select id,Name from User where UserRole.Name='ASM GJ' Order BY FirstName,LastName];
     list <User> sogj3=[select id,Name from User where UserRole.Name='DRM GJ' Order BY FirstName,LastName];
     list <User> sogj4=[select id,Name from User where UserRole.Name='RM GJ' Order BY FirstName,LastName];
    
    if(sogj1.size()>0){
       owner=sogj1.get(0).id;
    }
   
     else if(sogj1.size()<=0){
     
        if(sogj2.size()>0){
            owner=sogj2.get(0).id; 
        }
        
        else if(sogj2.size()<=0){

            if(sogj3.size()>0){
              owner=sogj2.get(0).id;  
             }
        
        else if(sogj3.size()<=0){

             if(sogj4.size()>0){
               owner=sogj4.get(0).id;   
             }   
        }
     }       
   }
    
    for(Account acc:changeOwner){

        if(sogj1!=null){
            acc.OwnerId=Owner;
        }
    }
    
     list <User> sohr1=[select id,Name from User where UserRole.Name='SO HR' Order BY FirstName,LastName];
     list <User> sohr2=[select id,Name from User where UserRole.Name='ASM HR' Order BY FirstName,LastName];
     list <User> sohr3=[select id,Name from User where UserRole.Name='DRM HR' Order BY FirstName,LastName];
     list <User> sohr4=[select id,Name from User where UserRole.Name='RM HR' Order BY FirstName,LastName];
    
     if(sohr1.size()>0){ 
       owner=sohr1.get(0).id;
    }
   
     else if(sohr1.size()<=0){
     
        if(sohr2.size()>0){
            owner=sohr2.get(0).id; 
        }
        
        else if(sohr2.size()<=0){

            if(sohr3.size()>0){
              owner=sohr2.get(0).id;  
             }
        
        else if(sohr3.size()<=0){

             if(sohr4.size()>0){
               owner=sohr4.get(0).id;   
             }   
        }
      }      
    }
    
    for(Account acc:changeOwner){
        
        if(sohr1!=null){
            acc.OwnerId=Owner;
        }
    }
    
     list <User> sojh1=[select id,Name from User where UserRole.Name='SO JH' Order BY FirstName,LastName];
     list <User> sojh2=[select id,Name from User where UserRole.Name='ASM JH' Order BY FirstName,LastName];
     list <User> sojh3=[select id,Name from User where UserRole.Name='DRM JH' Order BY FirstName,LastName];
     list <User> sojh4=[select id,Name from User where UserRole.Name='RM JH' Order BY FirstName,LastName];
    
    if(sojh1.size()>0){
          owner=sojh1.get(0).id;
     }
   
     else if(sojh1.size()<=0){
     
        if(sojh2.size()>0){
            owner=sojh2.get(0).id; 
        }
        
        else if(sojh2.size()<=0){

            if(sojh3.size()>0) {
              owner=sojh2.get(0).id;  
             }
        
        else if(sojh3.size()<=0){

             if(sojh4.size()>0){
               owner=sojh4.get(0).id;   
             }   
        }
      }     
    } 
    
   for(Account acc:changeOwner){

        if(sojh1!=null){
            acc.OwnerId=Owner;
        }
    }    
      
     list <User> somh1=[select id,Name from User where UserRole.Name='SO MH' Order BY FirstName,LastName];
     list <User> somh2=[select id,Name from User where UserRole.Name='ASM MH' Order BY FirstName,LastName];
     list <User> somh3=[select id,Name from User where UserRole.Name='DRM MH' Order BY FirstName,LastName];
     list <User> somh4=[select id,Name from User where UserRole.Name='RM MH' Order BY FirstName,LastName];
    
      if(somh1.size()>0){
        owner=somh1.get(0).id;
    }
   
     else if(somh1.size()<=0){
     
        if(somh2.size()>0){
            owner=somh2.get(0).id; 
        }
        
        else if(somh2.size()<=0) {

            if(somh3.size()>0){
              owner=somh2.get(0).id;  
             }
        
        else if(somh3.size()<=0){

             if(somh4.size()>0){
               owner=somh4.get(0).id;   
             }   
        }
      }     
    }
    
    for(Account acc:changeOwner){

        if(somh1!=null){
            acc.OwnerId=Owner;
        }
    } 
    
     list <User> somp1=[select id,Name from User where UserRole.Name='SO MP' Order BY FirstName,LastName];
     list <User> somp2=[select id,Name from User where UserRole.Name='ASM MP' Order BY FirstName,LastName];
     list <User> somp3=[select id,Name from User where UserRole.Name='DRM MP' Order BY FirstName,LastName];
     list <User> somp4=[select id,Name from User where UserRole.Name='RM MP' Order BY FirstName,LastName];    
    
    if(somp1.size()>0) {
         owner=somp1.get(0).id;
    }
   
     else if(somp1.size()<=0){
     
        if(somp2.size()>0){
            owner=somp2.get(0).id; 
        }
        
        else if(somp2.size()<=0){

            if(somp3.size()>0){
              owner=somp2.get(0).id;  
             }
        
        else if(somp3.size()<=0){

             if(somp4.size()>0){
               owner=somp4.get(0).id;   
             }   
        }
      }     
    } 
    
    for(Account acc:changeOwner){

        if(somp1!=null){
            acc.OwnerId=Owner;
        }
    }  
    
     list <User> soup1=[select id,Name from User where UserRole.Name='SO UP' Order BY FirstName,LastName];
     list <User> soup2=[select id,Name from User where UserRole.Name='ASM UP' Order BY FirstName,LastName];
     list <User> soup3=[select id,Name from User where UserRole.Name='DRM UP' Order BY FirstName,LastName];
     list <User> soup4=[select id,Name from User where UserRole.Name='RM UP' Order BY FirstName,LastName];
    
    
   if(soup1.size()>0){
        owner=soup1.get(0).id;
    }
   
     else if(soup1.size()<=0){
     
        if(soup2.size()>0){
            owner=soup2.get(0).id; 
        }
        
        else if(soup2.size()<=0){

            if(soup3.size()>0){
              owner=soup2.get(0).id;  
             }
        
        else if(soup3.size()<=0){

             if(soup4.size()>0){
               owner=soup4.get(0).id;   
             }   
        }
      }               
     } 
    
      
    for(Account acc:changeOwner){

        if(soup1!=null){
            acc.OwnerId=Owner;
        }
    }  



}
 
Sujeet PatelSujeet Patel
Hii
Neeraj
I think for condition checking you should use following technique
if(acc.Under__c=='Chhattisgarh'){
           role='SO CG';
           changeOwner.add(acc);
       }
      
       else if(acc.Under__c=='Bihar'){
           role='SO BI';
          changeOwner.add(acc);
       }

For Owner ID Assigning You sholud use following methodology
UserRole u=[select id,parentID,name from UserRole where Name=:role limit 1];
User us=[select id from User where UserRoleId=u.id];
if(us!=null){
	owner=us.id;
}
else if(us==null){
	us=[select id from User where UserRoleId=u.parentID];
	owner=us.id;
	if(us==null){
		us=[select id,name from User where UserRole.Name='RM CG'];
		owner=us.id;
	}
}
for(Account ac:changeOwner){
	if(us!=null){
		ac.OwnerID=owner;
		}
}

I hope this wil work fine accoding to your requirement
Neeraj Sharma 103Neeraj Sharma 103
hi  @Sujeet Patel 
thanks for your technquie its useful for me