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 so:



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 so:



    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 so:



      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 so:



      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










      asked 2 days ago









      Semmel

      579317




      579317






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          5
          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
            4
            down vote













            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




























              up vote
              4
              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
                2 days ago










              • 🤨 I saw that quick edit in the 5 minute window. :)
                – Daniel Ballinger
                2 days ago






              • 2




                Hah yes, just a formatting gaffe.
                – Charles T
                2 days ago











              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
              5
              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
                5
                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
                  5
                  down vote










                  up vote
                  5
                  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 yesterday

























                  answered 2 days ago









                  Jayant Das

                  10.2k2522




                  10.2k2522
























                      up vote
                      4
                      down vote













                      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

























                        up vote
                        4
                        down vote













                        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























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          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 2 days ago









                          Charles T

                          5,9371719




                          5,9371719






















                              up vote
                              4
                              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
                                2 days ago










                              • 🤨 I saw that quick edit in the 5 minute window. :)
                                – Daniel Ballinger
                                2 days ago






                              • 2




                                Hah yes, just a formatting gaffe.
                                – Charles T
                                2 days ago















                              up vote
                              4
                              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
                                2 days ago










                              • 🤨 I saw that quick edit in the 5 minute window. :)
                                – Daniel Ballinger
                                2 days ago






                              • 2




                                Hah yes, just a formatting gaffe.
                                – Charles T
                                2 days ago













                              up vote
                              4
                              down vote










                              up vote
                              4
                              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 yesterday

























                              answered 2 days ago









                              Daniel Ballinger

                              71.1k15146378




                              71.1k15146378








                              • 5




                                Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                                – Charles T
                                2 days ago










                              • 🤨 I saw that quick edit in the 5 minute window. :)
                                – Daniel Ballinger
                                2 days ago






                              • 2




                                Hah yes, just a formatting gaffe.
                                – Charles T
                                2 days ago














                              • 5




                                Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                                – Charles T
                                2 days ago










                              • 🤨 I saw that quick edit in the 5 minute window. :)
                                – Daniel Ballinger
                                2 days ago






                              • 2




                                Hah yes, just a formatting gaffe.
                                – Charles T
                                2 days ago








                              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
                              2 days ago




                              Ha! Beat you by 6 seconds. Just long enough to crack open a beer with my lovely deployment fish bottle opener :)
                              – Charles T
                              2 days ago












                              🤨 I saw that quick edit in the 5 minute window. :)
                              – Daniel Ballinger
                              2 days ago




                              🤨 I saw that quick edit in the 5 minute window. :)
                              – Daniel Ballinger
                              2 days ago




                              2




                              2




                              Hah yes, just a formatting gaffe.
                              – Charles T
                              2 days ago




                              Hah yes, just a formatting gaffe.
                              – Charles T
                              2 days ago


















                               

                              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

                              數位音樂下載

                              When can things happen in Etherscan, such as the picture below?

                              格利澤436b