• #17300
    Salesforce | Kumar Kumar #382
    Forcetalks

    How can I efficiently generate a Set from a List structure in salesforce?

    Hi all,

    I have a bit of code where I’ve written a SOQL query that returns a list of SObject records, however I need a Set<Id> structure to pass to another method that I don’t have control of (and thus can’t change the method signature of).

    How can I turn my returned List<SObject> into a Set<Id>? Is the best option just a for loop? Any guidance is appreciated.

    Thanks

    Tagged: , , , , , , , , ,

    #17943
    Salesforce | sushant sushant #384
    Forcetalks

    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

    #23137
    Salesforce | Charan Tej Charan Tej #35
    Forcetalks

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?