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
Prem ChauhanPrem Chauhan 

Find users under under in role hierarchy and update the same user on user picklist

Following is my requirement Please Help Me. 
I want to write an Apex Trigger based on Particular Role Hierarchy (XYZ is my role Name).  If the users are not blank in XYZ Role then fetch those users and add the same users in ABC object's field User(This is a custom picklist field of ABC object). 

Can anyone help me to achieve this  Many Thanks in advance. 

if we add/delete/Update user in XYZ roles then same will reflect on the ABC objects picklist fields named as User. 
edanna kedanna k
Hello Prem Chauhan,

Step 1 : Write a trigger on User object with events insert/update/delete.
Step 2 : Get your XYZ role id using query!
Id roleID = [SELECT Id, Name, DeveloperName 
FROM UserRole 
WHERE Name = 'XYZ ']
Step 3 : Check this role id is equal to user role id whome you are going to insert/update/delete!
for(User u :{
if(u.UserRoleId == roleID){
  ABC ab = new ABC();
  ab.user__c = u.Name;
Its just the way you can think! It may not work exactly!
And also you can refer :

Please let me know if it doesn't work!