Activity › Forums › Salesforce® Discussions › How can I efficiently generate a Set from a List structure in salesforce?

  • sushant

    Member
    January 17, 2017 at 2:40 PM

    Hi Kumar,

    You can do this like as follows:

    List<Account> accounts = [
    SELECT
    Id
    FROM
    Account
    ];

    Set<Id> accountIds = new Set<Id>();
    for(Account acc:accounts){
    accountIds.add(acc.Id);
    }

    Thanks

  • [adinserter block='9']
  • Charan Tej

    Member
    January 2, 2018 at 12:37 PM

    In addition to Susant anwer, you can also use this code.

    Map<Id, Account> mpAccounts = new Map<Id,Account>([Select id, name from Account]);
    You can use mpAccounts.keyset() which will give you a set collection of Ids.

Log In to reply.

Popular Salesforce Blogs

Top Salesforce Career Paths to Watch in 2026

Blog in

The Salesforce ecosystem continues to evolve rapidly, driven by advancements in AI, automation, data intelligence, and industry-specific cloud solutions. As businesses increasingly rely on Salesforce…

Popular Salesforce Videos