Select into Map using Database.query()





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






up vote
6
down vote

favorite












So we all know it is possible to directly select into a Map, like this:



Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


Is it possible to do the same thing, using Database.query()?



Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


Sadly the latter doesn't seem to work.










share|improve this question






























    up vote
    6
    down vote

    favorite












    So we all know it is possible to directly select into a Map, like this:



    Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


    Is it possible to do the same thing, using Database.query()?



    Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


    Sadly the latter doesn't seem to work.










    share|improve this question


























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      So we all know it is possible to directly select into a Map, like this:



      Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


      Is it possible to do the same thing, using Database.query()?



      Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


      Sadly the latter doesn't seem to work.










      share|improve this question















      So we all know it is possible to directly select into a Map, like this:



      Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


      Is it possible to do the same thing, using Database.query()?



      Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


      Sadly the latter doesn't seem to work.







      soql map dynamic-soql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday

























      asked Nov 15 at 17:44









      Semmel

      618417




      618417






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right.



          How about:



          Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));


          Any luck?






          share|improve this answer





















          • Since you were first by 6 seconds - I'll accept yours. :)
            – Semmel
            yesterday


















          up vote
          7
          down vote













          Adding a bit of info here on top of other answers.



          This works:



          Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


          Because the return type from the SOQL is that of Account.



          This does not work:



          Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


          Because the return type of Database.query is always SObject. So unless you cast the return type to the exact object type, it won't work.





          Working versions.



          Use SObject in your declaration



          Map<Id, SObject> accounts = new Map<Id, SObject>(Database.query('SELECT Id, Name FROM Account'));


          OR



          As in other answers, cast it to list/array of account:



          Map<Id, Account> accounts = new Map<Id, Account>((Account)Database.query('SELECT Id, Name FROM Account'));





          share|improve this answer






























            up vote
            6
            down vote













            If you cast it to a List of the expected sObject type first it will work.



            E.g.



            Map<Id, Account> accounts = 
            new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));





            share|improve this answer



















            • 5




              Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
              – Charles T
              Nov 15 at 17:47










            • 🤨 I saw that quick edit in the 5 minute window. :)
              – Daniel Ballinger
              Nov 15 at 17:48






            • 2




              Hah yes, just a formatting gaffe.
              – Charles T
              Nov 15 at 17:51










            • I could swear that I tried casting it but - but obviously I didn't. It's working now.
              – Semmel
              yesterday











            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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            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%2f239518%2fselect-into-map-using-database-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right.



            How about:



            Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));


            Any luck?






            share|improve this answer





















            • Since you were first by 6 seconds - I'll accept yours. :)
              – Semmel
              yesterday















            up vote
            7
            down vote



            accepted










            It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right.



            How about:



            Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));


            Any luck?






            share|improve this answer





















            • Since you were first by 6 seconds - I'll accept yours. :)
              – Semmel
              yesterday













            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right.



            How about:



            Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));


            Any luck?






            share|improve this answer












            It could be that this is one scenario where the "magic casting" of Database.query doesn't work quite right.



            How about:



            Map<Id, Account> accounts = new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));


            Any luck?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 at 17:46









            Charles T

            5,9921719




            5,9921719












            • Since you were first by 6 seconds - I'll accept yours. :)
              – Semmel
              yesterday


















            • Since you were first by 6 seconds - I'll accept yours. :)
              – Semmel
              yesterday
















            Since you were first by 6 seconds - I'll accept yours. :)
            – Semmel
            yesterday




            Since you were first by 6 seconds - I'll accept yours. :)
            – Semmel
            yesterday












            up vote
            7
            down vote













            Adding a bit of info here on top of other answers.



            This works:



            Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


            Because the return type from the SOQL is that of Account.



            This does not work:



            Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


            Because the return type of Database.query is always SObject. So unless you cast the return type to the exact object type, it won't work.





            Working versions.



            Use SObject in your declaration



            Map<Id, SObject> accounts = new Map<Id, SObject>(Database.query('SELECT Id, Name FROM Account'));


            OR



            As in other answers, cast it to list/array of account:



            Map<Id, Account> accounts = new Map<Id, Account>((Account)Database.query('SELECT Id, Name FROM Account'));





            share|improve this answer



























              up vote
              7
              down vote













              Adding a bit of info here on top of other answers.



              This works:



              Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


              Because the return type from the SOQL is that of Account.



              This does not work:



              Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


              Because the return type of Database.query is always SObject. So unless you cast the return type to the exact object type, it won't work.





              Working versions.



              Use SObject in your declaration



              Map<Id, SObject> accounts = new Map<Id, SObject>(Database.query('SELECT Id, Name FROM Account'));


              OR



              As in other answers, cast it to list/array of account:



              Map<Id, Account> accounts = new Map<Id, Account>((Account)Database.query('SELECT Id, Name FROM Account'));





              share|improve this answer

























                up vote
                7
                down vote










                up vote
                7
                down vote









                Adding a bit of info here on top of other answers.



                This works:



                Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


                Because the return type from the SOQL is that of Account.



                This does not work:



                Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


                Because the return type of Database.query is always SObject. So unless you cast the return type to the exact object type, it won't work.





                Working versions.



                Use SObject in your declaration



                Map<Id, SObject> accounts = new Map<Id, SObject>(Database.query('SELECT Id, Name FROM Account'));


                OR



                As in other answers, cast it to list/array of account:



                Map<Id, Account> accounts = new Map<Id, Account>((Account)Database.query('SELECT Id, Name FROM Account'));





                share|improve this answer














                Adding a bit of info here on top of other answers.



                This works:



                Map<Id, Account> accounts = new Map<Id, Account>([SELECT Id, Name FROM Account]);


                Because the return type from the SOQL is that of Account.



                This does not work:



                Map<Id, Account> accounts = new Map<Id, Account>(Database.query('SELECT Id, Name FROM Account'));


                Because the return type of Database.query is always SObject. So unless you cast the return type to the exact object type, it won't work.





                Working versions.



                Use SObject in your declaration



                Map<Id, SObject> accounts = new Map<Id, SObject>(Database.query('SELECT Id, Name FROM Account'));


                OR



                As in other answers, cast it to list/array of account:



                Map<Id, Account> accounts = new Map<Id, Account>((Account)Database.query('SELECT Id, Name FROM Account'));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 at 20:03

























                answered Nov 15 at 17:57









                Jayant Das

                10.3k2522




                10.3k2522






















                    up vote
                    6
                    down vote













                    If you cast it to a List of the expected sObject type first it will work.



                    E.g.



                    Map<Id, Account> accounts = 
                    new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));





                    share|improve this answer



















                    • 5




                      Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                      – Charles T
                      Nov 15 at 17:47










                    • 🤨 I saw that quick edit in the 5 minute window. :)
                      – Daniel Ballinger
                      Nov 15 at 17:48






                    • 2




                      Hah yes, just a formatting gaffe.
                      – Charles T
                      Nov 15 at 17:51










                    • I could swear that I tried casting it but - but obviously I didn't. It's working now.
                      – Semmel
                      yesterday















                    up vote
                    6
                    down vote













                    If you cast it to a List of the expected sObject type first it will work.



                    E.g.



                    Map<Id, Account> accounts = 
                    new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));





                    share|improve this answer



















                    • 5




                      Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                      – Charles T
                      Nov 15 at 17:47










                    • 🤨 I saw that quick edit in the 5 minute window. :)
                      – Daniel Ballinger
                      Nov 15 at 17:48






                    • 2




                      Hah yes, just a formatting gaffe.
                      – Charles T
                      Nov 15 at 17:51










                    • I could swear that I tried casting it but - but obviously I didn't. It's working now.
                      – Semmel
                      yesterday













                    up vote
                    6
                    down vote










                    up vote
                    6
                    down vote









                    If you cast it to a List of the expected sObject type first it will work.



                    E.g.



                    Map<Id, Account> accounts = 
                    new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));





                    share|improve this answer














                    If you cast it to a List of the expected sObject type first it will work.



                    E.g.



                    Map<Id, Account> accounts = 
                    new Map<Id, Account>((List<Account>)Database.query('SELECT Id, Name FROM Account'));






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 15 at 20:03

























                    answered Nov 15 at 17:46









                    Daniel Ballinger

                    71.3k15146379




                    71.3k15146379








                    • 5




                      Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                      – Charles T
                      Nov 15 at 17:47










                    • 🤨 I saw that quick edit in the 5 minute window. :)
                      – Daniel Ballinger
                      Nov 15 at 17:48






                    • 2




                      Hah yes, just a formatting gaffe.
                      – Charles T
                      Nov 15 at 17:51










                    • I could swear that I tried casting it but - but obviously I didn't. It's working now.
                      – Semmel
                      yesterday














                    • 5




                      Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                      – Charles T
                      Nov 15 at 17:47










                    • 🤨 I saw that quick edit in the 5 minute window. :)
                      – Daniel Ballinger
                      Nov 15 at 17:48






                    • 2




                      Hah yes, just a formatting gaffe.
                      – Charles T
                      Nov 15 at 17:51










                    • I could swear that I tried casting it but - but obviously I didn't. It's working now.
                      – Semmel
                      yesterday








                    5




                    5




                    Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                    – Charles T
                    Nov 15 at 17:47




                    Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                    – Charles T
                    Nov 15 at 17:47












                    🤨 I saw that quick edit in the 5 minute window. :)
                    – Daniel Ballinger
                    Nov 15 at 17:48




                    🤨 I saw that quick edit in the 5 minute window. :)
                    – Daniel Ballinger
                    Nov 15 at 17:48




                    2




                    2




                    Hah yes, just a formatting gaffe.
                    – Charles T
                    Nov 15 at 17:51




                    Hah yes, just a formatting gaffe.
                    – Charles T
                    Nov 15 at 17:51












                    I could swear that I tried casting it but - but obviously I didn't. It's working now.
                    – Semmel
                    yesterday




                    I could swear that I tried casting it but - but obviously I didn't. It's working now.
                    – Semmel
                    yesterday


















                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239518%2fselect-into-map-using-database-query%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?