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.
soql map dynamic-soql
add a comment |
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.
soql map dynamic-soql
add a comment |
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.
soql map dynamic-soql
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
soql map dynamic-soql
asked Nov 15 at 17:44
Semmel
579317
579317
add a comment |
add a comment |
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'));
add a comment |
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?
add a comment |
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'));
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
add a comment |
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'));
add a comment |
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'));
add a comment |
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'));
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 at 20:03
answered Nov 15 at 17:57
Jayant Das
10.2k2522
10.2k2522
add a comment |
add a comment |
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?
add a comment |
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?
add a comment |
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?
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 at 17:46
Charles T
5,9371719
5,9371719
add a comment |
add a comment |
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'));
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
add a comment |
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'));
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
add a comment |
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'));
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 at 20:03
answered Nov 15 at 17:46
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
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
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 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
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
add a comment |
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