2

This is more of a placeholder note for others who might stumble upon this apparent error in the Apex Compiler while coding a map <id,sobject> initialization using dynamic SOQL (such as when list of fields is dynamic).

Incorrect syntax - compiler blows up

map<ID,Account> res = 
   new Map<ID,Account> ((Account) Database.query('select id from Account'));

or (an even worse beginner mistake)

map<ID,Account> res = new Map<ID,Account> ('select id from Account');

An unexpected error has occurred. 2137292630-263233 (1873312459) for deploymentId=1drq0000000xEN2AAM If this persists, please contact customer support.

In Developer Console, above error message is preceded by 'Deployment Error"

The correct syntax (compiles fine)

map<ID,Account> res = 
            new Map<ID,Account> ((List<Account>) Database.query('select id from Account'));

The map initialization is expecting a list of SObjects of the proper type. Compiler error noted at V35. I looked in Known Issues and find it hard to believe I'm the first to see this.

1 Answer 1

1

You can cast a query result from dynamic apex as a single account

Account a = (Account) Database.query('SELECT Id FROM Account LIMIT 1');

You will of course get a run-time error if there is more than 1 account returned from the query

Account a2 = (Account) Database.query('SELECT Id FROM Account LIMIT 2');

FATAL_ERROR|System.QueryException: List has more than 1 row for assignment to SObject


The error you were seeing occurs when attempting to construct a map with a single record or a set rather than using a list or map. Map Constructors

Account a3 = new Account();
map<Id,Account> ma = new map<Id,Account>(a3);

UNKNOWN_EXCEPTION: An unexpected error occurred. Please include this ErrorId if you contact support: 1782200980-45844 (1670008057)

(This was run as Anonymous Apex, so the error message doesn't reference a deployment.)

3
  • 1
    yes, thank you for adding to the KB here at SFSE-- I was (apparently unsuccessfully) trying to make the point that the Apex compiler was blowing up when it shouldn't -- using the correct syntax works fine as you note; using the incorrect syntax should generate a meaningful compiler error message
    – cropredy
    Nov 25, 2015 at 1:57
  • @crop1645 Understood. I just wanted to clarify that the error was unrelated to deployment and dynamic SOQL.
    – martin
    Nov 25, 2015 at 2:05
  • excellent; I did my compile in DC and got 'Deployment Error' ; I'll change the title in the OP
    – cropredy
    Nov 25, 2015 at 2:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .