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
Sunil Kumar 81Sunil Kumar 81 

Trigger to validate user role.

I want to create a trigger which will validate the user role. Which means if role of new user is equals to its manager's role and new user's role id and its manager's roleid don't have a parent child relationship then it will show an exception. If any one hv success in same plz help..
Best Answer chosen by Sunil Kumar 81
gokul bharatigokul bharati
Hi Sunil,

Try this below snippet hope this helps.

trigger DontCreateUserRoleBelowManagerRole on User(before insert){
Map<Id,Id> RoleIDParentID=new Map<Id,Id>();
Map<Id,Id> managerIDRoleID=new Map<Id,Id>();
for(UserRole a:[select id,parentRoleID from UserRole]){
if(a.parentRoleID!=null){
RoleIDParentID.put(a.id,a.parentRoleID);
}
}
for(User a:[select id,UserRoleId from user]){
managerIDRoleID.put(a.id,a.UserRoleID);
}
if(Trigger.IsBefore&&Trigger.IsInsert){
 for(User a:Trigger.new){
 if(a.UserRoleID==managerIDRoleID.get(a.ManagerId)){
 a.addError('Manger Role and User role cant be same');
 }
 if(RoleIDParentID.get(a.UserRoleID)!=managerIDRoleID.get(a.managerId)){
 a.addError('User Role should be below Manager role');
 }
 }
 }
}

All Answers

mjohnson-TICmjohnson-TIC
Can you clarify a bit? Are you asking.

If
New User Role = Manager Role
Manager Role has no Child Roles
throw exception?
mjohnson-TICmjohnson-TIC
If it is exactly as you describe, this will work.
 
trigger UserTrigger on User (before insert) {
	for (Integer i = 0; i < trigger.new.size(); i++){
		if(trigger.new[i].ManagerId != null && trigger.new[i].UserRoleId != null){
			string managerrole;
			try{	
				managerrole = [select UserRoleId from User where Id=:trigger.new[i].ManagerId].UserRoleId;
			}catch(exception e){
				
			}
			if(managerrole == trigger.new[i].UserRoleId){
				try{
					string childrole = [Select Id from UserRole where ParentRoleId =: trigger.new[i].UserRoleId LIMIT 1].Id;
				}catch(exception e){
					trigger.new[i].addError('No child roles found for manager position');
				}
			}
		}		
	}	
}

 
gokul bharatigokul bharati
Hi Sunil,

Try this below snippet hope this helps.

trigger DontCreateUserRoleBelowManagerRole on User(before insert){
Map<Id,Id> RoleIDParentID=new Map<Id,Id>();
Map<Id,Id> managerIDRoleID=new Map<Id,Id>();
for(UserRole a:[select id,parentRoleID from UserRole]){
if(a.parentRoleID!=null){
RoleIDParentID.put(a.id,a.parentRoleID);
}
}
for(User a:[select id,UserRoleId from user]){
managerIDRoleID.put(a.id,a.UserRoleID);
}
if(Trigger.IsBefore&&Trigger.IsInsert){
 for(User a:Trigger.new){
 if(a.UserRoleID==managerIDRoleID.get(a.ManagerId)){
 a.addError('Manger Role and User role cant be same');
 }
 if(RoleIDParentID.get(a.UserRoleID)!=managerIDRoleID.get(a.managerId)){
 a.addError('User Role should be below Manager role');
 }
 }
 }
}
This was selected as the best answer
Sunil Kumar 81Sunil Kumar 81
thanxx to all... Problem has been solved...
Gokul  BharatiGokul Bharati
Hi Sunil,

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.

Thanks,
Gokul