Select into Map using Database.query()










6















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




























    6















    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


























      6












      6








      6








      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 Nov 20 '18 at 16:31







      Semmel

















      asked Nov 15 '18 at 17:44









      SemmelSemmel

      652417




      652417




















          3 Answers
          3






          active

          oldest

          votes


















          7














          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
            Nov 20 '18 at 10:23


















          7














          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
































            6














            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 '18 at 17:47











            • 🤨 I saw that quick edit in the 5 minute window. :)

              – Daniel Ballinger
              Nov 15 '18 at 17:48






            • 2





              Hah yes, just a formatting gaffe.

              – Charles T
              Nov 15 '18 at 17:51











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

              – Semmel
              Nov 20 '18 at 10:22










            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',
            autoActivateHeartbeat: false,
            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









            7














            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
              Nov 20 '18 at 10:23















            7














            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
              Nov 20 '18 at 10:23













            7












            7








            7







            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 '18 at 17:46









            Charles TCharles T

            6,5761923




            6,5761923












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

              – Semmel
              Nov 20 '18 at 10:23

















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

              – Semmel
              Nov 20 '18 at 10:23
















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

            – Semmel
            Nov 20 '18 at 10:23





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

            – Semmel
            Nov 20 '18 at 10:23













            7














            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





























              7














              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



























                7












                7








                7







                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 '18 at 20:03

























                answered Nov 15 '18 at 17:57









                Jayant DasJayant Das

                16.2k2926




                16.2k2926





















                    6














                    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 '18 at 17:47











                    • 🤨 I saw that quick edit in the 5 minute window. :)

                      – Daniel Ballinger
                      Nov 15 '18 at 17:48






                    • 2





                      Hah yes, just a formatting gaffe.

                      – Charles T
                      Nov 15 '18 at 17:51











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

                      – Semmel
                      Nov 20 '18 at 10:22















                    6














                    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 '18 at 17:47











                    • 🤨 I saw that quick edit in the 5 minute window. :)

                      – Daniel Ballinger
                      Nov 15 '18 at 17:48






                    • 2





                      Hah yes, just a formatting gaffe.

                      – Charles T
                      Nov 15 '18 at 17:51











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

                      – Semmel
                      Nov 20 '18 at 10:22













                    6












                    6








                    6







                    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 '18 at 20:03

























                    answered Nov 15 '18 at 17:46









                    Daniel BallingerDaniel Ballinger

                    73.7k15150399




                    73.7k15150399







                    • 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 '18 at 17:47











                    • 🤨 I saw that quick edit in the 5 minute window. :)

                      – Daniel Ballinger
                      Nov 15 '18 at 17:48






                    • 2





                      Hah yes, just a formatting gaffe.

                      – Charles T
                      Nov 15 '18 at 17:51











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

                      – Semmel
                      Nov 20 '18 at 10:22












                    • 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 '18 at 17:47











                    • 🤨 I saw that quick edit in the 5 minute window. :)

                      – Daniel Ballinger
                      Nov 15 '18 at 17:48






                    • 2





                      Hah yes, just a formatting gaffe.

                      – Charles T
                      Nov 15 '18 at 17:51











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

                      – Semmel
                      Nov 20 '18 at 10:22







                    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 '18 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 '18 at 17:47













                    🤨 I saw that quick edit in the 5 minute window. :)

                    – Daniel Ballinger
                    Nov 15 '18 at 17:48





                    🤨 I saw that quick edit in the 5 minute window. :)

                    – Daniel Ballinger
                    Nov 15 '18 at 17:48




                    2




                    2





                    Hah yes, just a formatting gaffe.

                    – Charles T
                    Nov 15 '18 at 17:51





                    Hah yes, just a formatting gaffe.

                    – Charles T
                    Nov 15 '18 at 17:51













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

                    – Semmel
                    Nov 20 '18 at 10:22





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

                    – Semmel
                    Nov 20 '18 at 10:22

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    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

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党