Select into Map using Database.query()
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
add a comment |
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
add a comment |
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
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
soql map dynamic-soql
edited Nov 20 '18 at 16:31
Semmel
asked Nov 15 '18 at 17:44
SemmelSemmel
652417
652417
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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?
Since you were first by 6 seconds - I'll accept yours. :)
– Semmel
Nov 20 '18 at 10:23
add a comment |
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'));
add a comment |
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'));
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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?
Since you were first by 6 seconds - I'll accept yours. :)
– Semmel
Nov 20 '18 at 10:23
add a comment |
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?
Since you were first by 6 seconds - I'll accept yours. :)
– Semmel
Nov 20 '18 at 10:23
add a comment |
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?
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?
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
add a comment |
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
add a comment |
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'));
add a comment |
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'));
add a comment |
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'));
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'));
edited Nov 15 '18 at 20:03
answered Nov 15 '18 at 17:57
Jayant DasJayant Das
16.2k2926
16.2k2926
add a comment |
add a comment |
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'));
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
add a comment |
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'));
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
add a comment |
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'));
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'));
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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