Skip to content

Instantly share code, notes, and snippets.

@kevanmoothien
Last active April 5, 2020 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevanmoothien/5dd03dd7374112a29f736069dba34316 to your computer and use it in GitHub Desktop.
Save kevanmoothien/5dd03dd7374112a29f736069dba34316 to your computer and use it in GitHub Desktop.
Get the list of record ids from Salesforce sobject records.

Common way

List<Contact> contacts = [Select Id, Name from Contact];
List<Id> contactIds = new List<Id>();
for(Contact c: contacts){
    contactIds.add(c.Id);
}

One line only

List<Contact> contacts = [Select Id, Name from Contact];
List<Id> contactIds = new List<Id>(new Map<Id, Contact>(contacts).keySet());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment