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
Bhavesh RekvarBhavesh Rekvar 

How to get all users using Apex code

Hello Salesforce team,

I have tried this below apex code but this is for account but i don't know how we can get the all the user can you provide me whole code of get all user using apex code 

 

@RestResource(urlMapping='/AccountService/*')
global class AccountService{
@HttpGet global static List getAccounts() {
List Accounts = [SELECT Id,Name from Account LIMIT 10];
return Accounts;
}
}

Above code for get account but i need to get all added users in salesforce.

I don't know what is replace this line @RestResource(urlMapping='/AccountService/*')

Please provide me whole code of how to get all users of salesforce, Don't provide any blog and example because i have already tried it so if can possible to share whole code of get all users of salesforce.

One more think, I want talk to you, some question and queries related to salesforce so we can set a meeting or not.

If can possible so please let me know...

Prasanna Kumar Tummala 2Prasanna Kumar Tummala 2

Hello Bhavesh,

You can retrieve the list of users by below simple query

List<User> Usersdetails = [SELECT Id,name,ProfileId,IsActive FROM User ];

If you want to retrieve only active users in your organization simply use the where condition, WHERE isActive= False.

For more details, drop us a note at https://www.etggs.com/contact-etg/
Thanks,
Prasanna Kumar Tummala
ETG Global Services
www.etggs.com

sachinarorasfsachinarorasf
Hi Bhavesh,

You can use the below code snippets


@RestResource(urlMapping='/User/*')
global class UserListClass{
@HttpGet global static List getUser() {

Map<Id,Profile> profileIds = new Map<id,profile>([SELECT Id,UserLicenseId FROM Profile where UserLicenseId  in (SELECT Id FROM UserLicense where name ='Salesforce')]);

List<user> standardProfileUsers = [select id from user where profileId in:profileIds.Keyset()];

return standardProfileUsers;

}
}

The urlMapping 
property allows us to set path where the service will be available.
This is how we set up the endpoint for the service. 
https://<myInstance>.salesforce.com/services/apexrest/User
*you can replace it with your own end point.

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com