You need to sign in to do that
Don't have an account?
Retrieve UserRoleID
This is the SOQL used in a trigger
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.
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.
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
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
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.
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.
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
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' )