question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improved Apex Language Support for ".apex" execute anonymous files

See original GitHub issue

Summary

When writing Apex in a .apex file to be used for anonymous execution, the Apex language server marks the first line of code as invalid with an “Unexpected token” error. It seems the language server expects a class or trigger keyword and does not know what to do with simple lines of Apex outside that context.

Steps To Reproduce:

  1. Create an .apex extension file inside a dx project
  2. Write a single line of apex like so: System.debug('Hello World');

Expected result

No error message appears in the “Problems” output panel.

Actual result

The following error message appears in the “Problems” output panel: Unexpected Token: 'System.debug'. (1,1).

Additional information

screen shot 2018-05-25 at 2 01 56 pm

VS Code Version: 1.23.1

SFDX CLI Version: 6.17.0-b1f53723b9

OS and version: macOS High Sierra 10.13.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vazexqicommented, May 31, 2018

@ntotten - Yes, we need to make changes to the Apex LS to make it understand execute anonymous – I don’t think it’s big but it needs to be done.

As you can see in the screenshot above in the issue, the LS is saying there is an error in that file. That’s because it’s treating .apex as a .cls file and the only top-level constructs allowed in a .cls file are class|enum|interface declaration. You cannot have standalone statements like you can with execute anonymous.

I’m not opposed to doing this. I just want to know if .apex is the extension convention that we want to adhere to. It’s not documented anywhere in our docs.

0reactions
smaddox-sfcommented, Mar 29, 2021

Belatedly closing this. The original issue has been resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improved Apex Language Support for ".apex" execute ...
apex file to be used for anonymous execution, the Apex language server marks the first line of code as invalid with an "Unexpected...
Read more >
Executing Anonymous Apex Code - Salesforce Help
The Execute Anonymous Apex tool in the Developer Console runs the Apex code you enter using ExecuteAnonymous and generates a debug log with...
Read more >
Apex: Basics and Anonymous Execution - YouTube
In this Quick Take, we'll get you started with Apex and see how and where to write Apex code. You'll learn how to...
Read more >
Create a class using execute anonymous
If you create a apex file and drop your code in there, it'll give you error messages to show you where you are...
Read more >
Apex Developer Guide - Salesforce Implementation guides
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found