How can I initialise an apex Set, Set or Set from SOQL in Apex Code?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












Is this possible? I would like to do the following directly:



Set<String> titles = new Set<String>([Select Title From SObject]); 






share|improve this question
















  • 3




    this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
    – Programatic
    Sep 5 at 14:15
















up vote
2
down vote

favorite
1












Is this possible? I would like to do the following directly:



Set<String> titles = new Set<String>([Select Title From SObject]); 






share|improve this question
















  • 3




    this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
    – Programatic
    Sep 5 at 14:15












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





Is this possible? I would like to do the following directly:



Set<String> titles = new Set<String>([Select Title From SObject]); 






share|improve this question












Is this possible? I would like to do the following directly:



Set<String> titles = new Set<String>([Select Title From SObject]); 








share|improve this question











share|improve this question




share|improve this question










asked Sep 5 at 14:08









KennyBartMan

1807




1807







  • 3




    this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
    – Programatic
    Sep 5 at 14:15












  • 3




    this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
    – Programatic
    Sep 5 at 14:15







3




3




this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15




this functionality doesn't exist unfortunately. You will have to iterate over the list of SObjects and manually add the title fields to the set.
– Programatic
Sep 5 at 14:15










2 Answers
2






active

oldest

votes

















up vote
7
down vote



accepted










No it is not possible except for Set<Id>. With other types, you need a for loop:



Set<String> names = new Set<String>();
for (MyObject__c record : [SELECT Name FROM MyObject__c])
names.add(record.Name);


The special case is that with a record's own Id, you can use this Map constructor:



Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();


You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.



Set<Id> parentIds = new Map<Id, SObject>([
SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
// ^^ you can now call .get('Id') on a result record
]).keySet();





share|improve this answer






















  • Missing comma between Parent__c and Id in the SOQL query.
    – Tad
    Sep 5 at 17:15






  • 1




    No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
    – Adrian Larson♦
    Sep 5 at 17:17











  • I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
    – Tad
    Sep 5 at 17:20







  • 2




    I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
    – Adrian Larson♦
    Sep 5 at 17:21











  • Ahhhh... I see now. My fault. Interesting solution.
    – Tad
    Sep 5 at 17:22


















up vote
3
down vote













You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-



Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();


Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.



Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.



Set<String> titles = new Set<String>();
for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
titles.add((String)ar.get('Title'));






share|improve this answer








New contributor




Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231330%2fhow-can-i-initialise-an-apex-setstring-setid-or-setdate-from-soql-in-apex%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote



    accepted










    No it is not possible except for Set<Id>. With other types, you need a for loop:



    Set<String> names = new Set<String>();
    for (MyObject__c record : [SELECT Name FROM MyObject__c])
    names.add(record.Name);


    The special case is that with a record's own Id, you can use this Map constructor:



    Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();


    You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.



    Set<Id> parentIds = new Map<Id, SObject>([
    SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
    // ^^ you can now call .get('Id') on a result record
    ]).keySet();





    share|improve this answer






















    • Missing comma between Parent__c and Id in the SOQL query.
      – Tad
      Sep 5 at 17:15






    • 1




      No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
      – Adrian Larson♦
      Sep 5 at 17:17











    • I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
      – Tad
      Sep 5 at 17:20







    • 2




      I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
      – Adrian Larson♦
      Sep 5 at 17:21











    • Ahhhh... I see now. My fault. Interesting solution.
      – Tad
      Sep 5 at 17:22















    up vote
    7
    down vote



    accepted










    No it is not possible except for Set<Id>. With other types, you need a for loop:



    Set<String> names = new Set<String>();
    for (MyObject__c record : [SELECT Name FROM MyObject__c])
    names.add(record.Name);


    The special case is that with a record's own Id, you can use this Map constructor:



    Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();


    You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.



    Set<Id> parentIds = new Map<Id, SObject>([
    SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
    // ^^ you can now call .get('Id') on a result record
    ]).keySet();





    share|improve this answer






















    • Missing comma between Parent__c and Id in the SOQL query.
      – Tad
      Sep 5 at 17:15






    • 1




      No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
      – Adrian Larson♦
      Sep 5 at 17:17











    • I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
      – Tad
      Sep 5 at 17:20







    • 2




      I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
      – Adrian Larson♦
      Sep 5 at 17:21











    • Ahhhh... I see now. My fault. Interesting solution.
      – Tad
      Sep 5 at 17:22













    up vote
    7
    down vote



    accepted







    up vote
    7
    down vote



    accepted






    No it is not possible except for Set<Id>. With other types, you need a for loop:



    Set<String> names = new Set<String>();
    for (MyObject__c record : [SELECT Name FROM MyObject__c])
    names.add(record.Name);


    The special case is that with a record's own Id, you can use this Map constructor:



    Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();


    You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.



    Set<Id> parentIds = new Map<Id, SObject>([
    SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
    // ^^ you can now call .get('Id') on a result record
    ]).keySet();





    share|improve this answer














    No it is not possible except for Set<Id>. With other types, you need a for loop:



    Set<String> names = new Set<String>();
    for (MyObject__c record : [SELECT Name FROM MyObject__c])
    names.add(record.Name);


    The special case is that with a record's own Id, you can use this Map constructor:



    Set<Id> ids = new Map<Id, SObject>([/*query*/]).keySet();


    You can abuse this shortcut and field alias functionality in aggregate queries to get a collection of parent Ids also.



    Set<Id> parentIds = new Map<Id, SObject>([
    SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c
    // ^^ you can now call .get('Id') on a result record
    ]).keySet();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 5 at 17:22

























    answered Sep 5 at 14:42









    Adrian Larson♦

    100k19106224




    100k19106224











    • Missing comma between Parent__c and Id in the SOQL query.
      – Tad
      Sep 5 at 17:15






    • 1




      No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
      – Adrian Larson♦
      Sep 5 at 17:17











    • I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
      – Tad
      Sep 5 at 17:20







    • 2




      I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
      – Adrian Larson♦
      Sep 5 at 17:21











    • Ahhhh... I see now. My fault. Interesting solution.
      – Tad
      Sep 5 at 17:22

















    • Missing comma between Parent__c and Id in the SOQL query.
      – Tad
      Sep 5 at 17:15






    • 1




      No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
      – Adrian Larson♦
      Sep 5 at 17:17











    • I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
      – Tad
      Sep 5 at 17:20







    • 2




      I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
      – Adrian Larson♦
      Sep 5 at 17:21











    • Ahhhh... I see now. My fault. Interesting solution.
      – Tad
      Sep 5 at 17:22
















    Missing comma between Parent__c and Id in the SOQL query.
    – Tad
    Sep 5 at 17:15




    Missing comma between Parent__c and Id in the SOQL query.
    – Tad
    Sep 5 at 17:15




    1




    1




    No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
    – Adrian Larson♦
    Sep 5 at 17:17





    No. That is the whole point of including that query. What it was actually missing is the GROUP BY clause.
    – Adrian Larson♦
    Sep 5 at 17:17













    I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
    – Tad
    Sep 5 at 17:20





    I don't understand. You're saying you intentionally left out the comma between Parent__c and Id in your example. This query as is won't work without a comma between the fields. SELECT Parent__c Id FROM MyObject__c GROUP BY Parent__c Adding a comma will make it work. SELECT Parent__c, Id FROM MyObject__c GROUP BY Parent__c If I'm not following then it might be that your explanation needs a bit more clarification as to why you left it out.
    – Tad
    Sep 5 at 17:20





    2




    2




    I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
    – Adrian Larson♦
    Sep 5 at 17:21





    I didn't leave anything out. I am not showing how to query two separate fields. I am using alias functionally.
    – Adrian Larson♦
    Sep 5 at 17:21













    Ahhhh... I see now. My fault. Interesting solution.
    – Tad
    Sep 5 at 17:22





    Ahhhh... I see now. My fault. Interesting solution.
    – Tad
    Sep 5 at 17:22













    up vote
    3
    down vote













    You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-



    Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();


    Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.



    Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.



    Set<String> titles = new Set<String>();
    for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
    titles.add((String)ar.get('Title'));






    share|improve this answer








    New contributor




    Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote













      You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-



      Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();


      Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.



      Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.



      Set<String> titles = new Set<String>();
      for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
      titles.add((String)ar.get('Title'));






      share|improve this answer








      New contributor




      Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        3
        down vote










        up vote
        3
        down vote









        You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-



        Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();


        Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.



        Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.



        Set<String> titles = new Set<String>();
        for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
        titles.add((String)ar.get('Title'));






        share|improve this answer








        New contributor




        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        You can do it fairly easily for the Set of Id use case by passing the SOQL to the constructor of an Id to Sobject Map and then grabbing the keyset from this;-



        Set<Id> Ids = new Map<Id, Sobject>([SELECT Id FROM SObject]).keySet();


        Other than that though there isn't a direct way to do it for non-Id fields. Your best bet would probably to create a reusable worker method to handle this.



        Depending on what you need this for aggregate SOQL functions may help as they will produce a list of all unique values for the aggregated field.



        Set<String> titles = new Set<String>();
        for(AggregateResult ar : [SELECT Title FROM SObject GROUP BY Title])
        titles.add((String)ar.get('Title'));







        share|improve this answer








        New contributor




        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Sep 5 at 14:41









        Alex 255

        312




        312




        New contributor




        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Alex 255 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231330%2fhow-can-i-initialise-an-apex-setstring-setid-or-setdate-from-soql-in-apex%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery