Are you facing Unexpected Token ERROR?

3.9K    Asked by AnushaAcharya in Salesforce , Asked on Apr 22, 2021
Answered by Ankit Chauhan

Please copy and paste your code into your question and use the {} tool or Ctrl-K to format it appropriately as code if you are facing an unexpected token. Screenshots are generally harder to work with. In this case, you have two simple syntax errors:

public Map 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 oppMapMethod() {
The other issue is that your object creation syntax is wrong.
Map oppMap = new Map

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 oppMap = new Map();
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 oppMap = new Map([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([SELECT Id, .... FROM Opportunity ...]);

Hope this helps you!



Your Answer

Answer (1)

The "unexpected token" error generally indicates a syntax error in the code. This can happen in various programming environments, including SQL, JavaScript, Python, etc. To provide a more specific solution, I'll need details about the context in which you're encountering this error. Here are some common scenarios and their potential fixes:

1. SQL

If you encounter an unexpected token error in SQL, it could be due to syntax mistakes such as missing keywords, incorrect order of clauses, or missing punctuation.

  -- Incorrect SQLSELECT * FORM table_name;-- Correct SQLSELECT * FROM table_name;

2. JavaScript

In JavaScript, an unexpected token error often occurs due to syntax errors such as missing or misplaced punctuation, incorrect use of operators, or invalid code structure.

  // Incorrect JavaScriptif (true {   console.log("Hello");}// Correct JavaScriptif (true) {  console.log("Hello");}3. PythonIn Python, unexpected token errors usually arise from improper indentation, missing colons, or incorrect syntax.pythonCopy code# Incorrect Pythonif True  print("Hello")# Correct Pythonif True:  print("Hello")

4. HTML

In HTML, an unexpected token error can occur if there's a typo in the tags or attributes, or if the structure is not correctly formed.


5. JSON

In JSON, an unexpected token error typically happens when there's a syntax mistake such as missing commas, incorrect use of quotes, or an invalid structure.

  // Incorrect JSON{  "name": "John",  "age": 30  "city": "New York"}// Correct JSON{  "name": "John",  "age": 30,  "city": "New York"}

Steps to Resolve:

Check the Syntax: Review the code or query for any syntax errors. Ensure all brackets, quotes, and keywords are correctly placed.

Use a Linter or Formatter: Use tools like ESLint for JavaScript, PEP8 for Python, or SQL linters to identify and correct syntax errors.

Read Error Messages Carefully: The error messages often provide a line number and a character position where the error is detected, which can help in pinpointing the issue.

Refer to Documentation: Consult the documentation for the language or tool you are using to understand the correct syntax and usage.

If you provide more details about the specific context and code where you are facing the unexpected token error, I can offer a more targeted solution.

5 Months

Interviews

Parent Categories