+ Start a Discussion
RovRov 

Retrieve UserRoleID

This is the SOQL used in a trigger
 
List<User> usrlst = [SELECT Id FROM User WHERE UserRole.DeveloperName LIKE : '%Customer_Support_Rep%' and IsActive = True];


Instead of UserRole.DeveloperName i would like to use the UserRoleId = '78hjhajhjhjhj'

Hardcoding id is not a good practice.

I would like to store the RoleId in same place like custom setting or custommetadat type setting.

However i don't know how to access the value from the custom setting or custom metadata setting.

Can someone help or point to an example?

Thanks.

 
Best Answer chosen by Rov
RovRov
I was able to work through a solution as follows 

1. Created  a new Custom Metadata Type Setting
2. Created a new field to capture the RoleId
3. Created a new record to store the RoleId

Queried the record in Apex Class as seen here 
 
String CSR_RoleID = [select RoleId__c from Id_Store__mdt Where DeveloperName = 'Customer_Support_Rep'].RoleId__c;

system.debug('>>>>>>>>>>>> ' +BDR_RoleID);

List<User> usrlst = [SELECT Id, Name FROM User WHERE UserRole.id = :CSR_RoleID and IsActive = True];

for (User u : usrlst){ 
  
system.debug('The users in the CSR role are ' +u.Name);

}


The advantages of storing the RoleID in Custom Metadata Type settings (CMDTS) - 
1. No hardcoding id 
2. When the Role Name changes for what ever reason, the id will remain intact.
3. Can create records in CMDTS to store many other Id's
​​​​​​​(That's the reason I called the CMDT as 'ID Store'  )

All Answers

Malika Pathak 9Malika Pathak 9
Hi Skyh,
public static List<user> fetchUserDetails(Id userId){

    List<User> usrlst = [SELECT Id FROM User WHERE Id=:userId and IsActive = True];
}

for your reference, you can this link which provide you a better understanding of user and user role
See Link: https://salesforce.stackexchange.com/questions/201418/navigating-from-a-userid-to-userroleid-in-a-custom-controller

if you find your Solution then mark as the best answer as well 
thanks and Regards
Malika Pathak.
RovRov

Thanks for the reponse Malika, but you misunderstood my question. 

I am not asking about getting a specific User's UserRoleID. 

I am asking about fetching all Users that belong to a certain Role. However, instead of saying give me all users Where  RoleName = 'Customer_Support_Rep' , i  want to say, give me all users Where RoleId = '01nblahblahabah' . The reason i want to fetch by RoleID is that, if a System Admin changes the UserRole Names (labels as well as Role Names ),every class/trigger/VFP where we referenced the RoleName/Role DeveloperName would fail. This means, making the changes in the code and deploying the change resulting in Developer's resource and time. Thus, the best bet is fetching records by Id instead of labels/Names.  Since it is not a good practice to hardcode id's, the better solution suggested was storing the ID in Custom Meta Data Setting Type, and querying it to be referenced in the code, which is what i ended by doing as documented in my comment.  

RovRov
I was able to work through a solution as follows 

1. Created  a new Custom Metadata Type Setting
2. Created a new field to capture the RoleId
3. Created a new record to store the RoleId

Queried the record in Apex Class as seen here 
 
String CSR_RoleID = [select RoleId__c from Id_Store__mdt Where DeveloperName = 'Customer_Support_Rep'].RoleId__c;

system.debug('>>>>>>>>>>>> ' +BDR_RoleID);

List<User> usrlst = [SELECT Id, Name FROM User WHERE UserRole.id = :CSR_RoleID and IsActive = True];

for (User u : usrlst){ 
  
system.debug('The users in the CSR role are ' +u.Name);

}


The advantages of storing the RoleID in Custom Metadata Type settings (CMDTS) - 
1. No hardcoding id 
2. When the Role Name changes for what ever reason, the id will remain intact.
3. Can create records in CMDTS to store many other Id's
​​​​​​​(That's the reason I called the CMDT as 'ID Store'  )
This was selected as the best answer