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
eswarieswari 

users present in salesforce

Hi,

 

How can I get the list of available users, user roles and salesforce user attributes in my apex class?

Navatar_DbSupNavatar_DbSup

Hi,

You can query to get list of user as below;

List<user> us=[select name,id from user ];


UserRole ur=[Select u.CaseAccessForAccountOwner, u.ForecastUserId, u.Id, u.LastModifiedById,u.LastModifiedBy.Alias, u.LastModifiedBy.CallCenterId, u.LastModifiedBy.City, u.LastModifiedBy.CompanyName, u.LastModifiedBy.ContactId, from UserRole u];


Id roleId = [select UserRoleId from User where Id = :userId].UserRoleId;

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

eswarieswari

Thanks for the reply.

 

I am able to get the available users list now.

 

Can I get the list of all available roles in salesforce (not only available user role)  as well?

Devendra@SFDCDevendra@SFDC

 

To retrieve all roles, try this:

 

List<UserRole> lstUserRoles=[Select id,Name from UserRole];

 

Hope this helps.

 

Thanks and Regards,

Devendra S

eswarieswari

Thank you. I am able to get the roles as well.

 

Similarly, Can I get the salesforce user attribute names (not values) such as username, email, firstname, lastname, etc..

Devendra@SFDCDevendra@SFDC

 

Hi,

 

Try this,

 

List<User> lstUser=[Select id,name,Username,Email,FirstName,LastName from User];

 

similary you can query for other attributes of User.

 

Hope this helps.

 

Regards,

Devendra S

 

If any of my replies/answers work for you, do mark them as 'Solution' as it may help others.

eswarieswari

Thanks for your reply.

 

I need to get the user attribute names present in salesforce to search user.

Navatar_DbSupNavatar_DbSup

Hi,

 

Here User Id is the unique attribute to search a user in salesforce. So you can query user id and then can search in salesforce as below:

 

List<User> u= [select id from user where email=’abc@yahoo.com’];

 

 You can search a user in salesforce by email, username attribute of user too.