2

enter image description here

Any thoughts on why the errors?

2 Answers 2

5

Please copy and paste your code into your question and use the {} tool or Ctrl-K to format it appropriately as code. Screenshots are generally harder to work with.

In this case, you have two simple syntax errors:

public Map<Id, Opportunity> oppMapMethod {

This method declaration is missing its parameter list, which in this case is empty. You must include an empty parameter list () in any method declaration that doesn't take parameters, the only exception being a getter/setter on a property. Hence:

public Map<Id, Opportunity> oppMapMethod() {

The other issue is that your object creation syntax is wrong.

Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>

Object constructors are method calls, and also require parentheses as such. Additionally, all statements in Apex end with a semicolon, as in most C/Java family languages.

Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();

However....

You don't have to do most of this code anyway, because Apex has a "magic" syntax for initializing sObject maps of Id to sObject. Just pass the SOQL-query list directly to the Map constructor:

Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([SELECT Id, .... FROM Opportunity ...]);

Then you don't need a separate list or for loop at all, and your method becomes a one-liner:

return new Map<Id, Opportunity>([SELECT Id, .... FROM Opportunity ...]);
1
  • Given that you can reduce it to a 1 liner, I don't think you really need this method. May 13, 2019 at 1:54
0

These are the syntax issues basically:

1. Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();

2. List<Opportunity> oppList = new List<Opportunity>([Select Id, name from Opportunity]);

3. public Map<Id, Opportunity> oppMapMethod(){

   }
1
  • 1
    (2) is actually not a mistake - there's no need to use the List constructor when assigning the results of a SOQL query.
    – David Reed
    Sep 4, 2018 at 13:10

You must log in to answer this question.

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