+ Start a Discussion
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Validation on Role can be assigned to only one user

Hi,
  
   We have two 3 roles ( Finance Approver , GHC Approver  , Program Administrator ) This can be assinged to only one user it cannot be assigned to multiple users we want to add a validation rule if this role is already assigned need to display a message in user screen please suggest me how to write this validation rule 

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
ManojjenaManojjena
Hi Sudhir ,

Try with below code !
 
trigger roleCheck on User (before insert, before update){
    Set<String> roleNameSet=new Set<String>{'Finance Approver' , 'GHC Approver ' , 'Program Administrator'};
	Set<Id> userRoleIdSet=new Set<Id>();
	for(userRole useRol :[SELECT id  FROM UserRole  WHERE Name IN: roleNameSet]){
		userRoleIdSet.add(useRol.Id);
	}
	if(trigger.isInert){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId)){
				usr.adderror('Role Aleready Assigned, select new Role');
			}
		}
	}if(Trigger.isUpdate){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId) && usr.UserRoleId != Trigger.oldMap.get(usr.Id).UserRoleId ){
				usr.adderror('Role aleready assigned, select new Role');
			}
		}
	}
}
Let me know if it helps !!
Thanks
Manoj
 

All Answers

MagulanDuraipandianMagulanDuraipandian
We can't use Validation rule for this.

Use trigger.

www.infallibletechie.com
ManojjenaManojjena
Hi Sudhirn,

Yes it is correct that this i snot possible in Validation rule .You need to write trigger ,You have to write trigger for before insert and updat eto check this validation .

You can try with below trigger it will help you to solve your problem .
 
trigger roleCheck on User (before insert, before update){
    Set<String> roleNameSet=new Set<String>{'Finance Approver' , 'GHC Approver ' , 'Program Administrator'};
	Set<Id> userRoleIdSet=new Set<Id>();
	for(userRole useRol :[SELECT id,UserRoleId from userRole  WHERE Name IN: roleNameSet]){
		userRoleIdSet.add(useRol.Id);
	}
	if(trigger.isInert){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId)){
				usr.adderror('Role Aleready Assigned, select new Role');
			}
		}
	}if(Trigger.isUpdate){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId) && usr.UserRoleId != Trigger.oldMap.get(usr.Id).UserRoleId ){
				usr.adderror('Role aleready assigned, select new Role');
			}
		}
	}
}
Let me know if it helps !!
Thanks
Manoj
 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Manjo, 


   I tried your method but UserRoleId  column do not exist in userrole object  I tried modifiing the code but no luck its not working 
 
trigger roleCheck on User (before insert, before update){
    Set<String> roleNameSet=new Set<String>{'Finance Approver','GHC Approver','Program Administrator'};
    Set<String> userRoleIdSet=new Set<String>();
    
    
    for(userRole useRol :[SELECT id,name from userRole  WHERE Name IN: roleNameSet]){
        userRoleIdSet.add(useRol.name);
    }
    if(trigger.isInsert){
        for(User usr : Trigger.new){
            if(userRoleIdSet.contains(usr.name)){
                usr.adderror('Role Aleready Assigned, select new Role');
            }
        }
    }if(Trigger.isUpdate){
        for(User usr : Trigger.new){
            if(userRoleIdSet.contains(usr.name) && usr.UserRoleId != Trigger.oldMap.get(usr.Id).name){
                usr.adderror('Role aleready assigned, select new Role');
            }
        }
    }
}

Please suggest me how to change the code. 

Thanks
Sudhir


 
ManojjenaManojjena
Hi Sudhir ,

Try with below code !
 
trigger roleCheck on User (before insert, before update){
    Set<String> roleNameSet=new Set<String>{'Finance Approver' , 'GHC Approver ' , 'Program Administrator'};
	Set<Id> userRoleIdSet=new Set<Id>();
	for(userRole useRol :[SELECT id  FROM UserRole  WHERE Name IN: roleNameSet]){
		userRoleIdSet.add(useRol.Id);
	}
	if(trigger.isInert){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId)){
				usr.adderror('Role Aleready Assigned, select new Role');
			}
		}
	}if(Trigger.isUpdate){
		for(User usr : Trigger.new){
			if(userRoleIdSet.contains(usr.UserRoleId) && usr.UserRoleId != Trigger.oldMap.get(usr.Id).UserRoleId ){
				usr.adderror('Role aleready assigned, select new Role');
			}
		}
	}
}
Let me know if it helps !!
Thanks
Manoj
 
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com

Thanks Manjo It worked can you please help to prepare test class for the trigger you created this would be great help for me 
 

Thanks

Sudhir

ManojjenaManojjena
Hi Sudhir,

Please try with below trigger and test class !!
trigger roleCheck on User (before insert, before update){
    Set<String> roleNameSet=new Set<String>{'Finance Approver' , 'GHC Approver ' , 'Program Administrator'};
	Set<Id> userRoleIdSet=new Set<Id>();
	Set<Id> userRoleIdSetToChk=new Set<Id>();
	for(userRole useRol :[SELECT id  FROM UserRole  WHERE Name IN: roleNameSet]){
		userRoleIdSet.add(useRol.Id);
	}
	if(!userRoleIdSet.isEmpty()){
		for(User us :[SELECT id,UserRoleId FROM User WHERE UserRoleId IN : userRoleIdSet]){
	       userRoleIdSetToChk.add(us.UserRoleId);
		}
		if(trigger.isInert){
			for(User usr : Trigger.new){
				if(userRoleIdSetToChk.contains(usr.UserRoleId)){
					usr.adderror('Role Aleready Assigned, select new Role');
				}
			}
		}if(Trigger.isUpdate){
			for(User usr : Trigger.new){
				if(userRoleIdSetToChk.contains(usr.UserRoleId) && usr.UserRoleId != Trigger.oldMap.get(usr.Id).UserRoleId ){
					usr.adderror('Role aleready assigned, select new Role');
				}
			}
		}
	}
}
@isTest
public class RoleCheckTest{
  public static testMethod void unitTest(){
     Profile prof=[SELECT id,Name FROM Profile WHERE Name='System Administrator'];
     UserRole ur = new UserRole();
        ur.Name='Finance';
        insert ur;
    UserRole urr = new UserRole();
        urr.Name='GHC Appro';
        insert urr;
	User usr=new User();
        usr.ProfileId=prof.Id;
        usr.Alias = 'standt';
        usr.Email='testduser@testorg.com';
        usr.EmailEncodingKey='UTF-8';
        usr.LastName='Testing';
        usr.LanguageLocaleKey='en_US';
        usr.LocaleSidKey='en_US';
        usr.TimeZoneSidKey='America/Los_Angeles';
        usr.UserName='adminusr@testorg.com'; 
        usr.UserRoleId=ur.Id;
        Insert usr;
        usr.UserRoleId=urr.Id; 
        update usr;
    }
}

Let me know if it works !!
Thanks
Manoj
 
Rajkumar SRajkumar S
Hi,
     My scenario is for certain Roles(say VP,Marketing) there should be only one user assigned. we can restrict user creation by triggers but how can i achieve this while assigning the user in Role hierarchy. If i assign more than one user in the Role Hierarchy and while saving, it should display an error message here also. 

Kindly give me some pointers on how to achieve this. 


User-added image

Thanks,
Raj