2

I'm looking for some solution, which helps me to write less code in Apex. Is there any tool like Lombok for Java.

4
  • 3
    If you use the right IDE (such as Illuminated Cloud in IntelliJ IDEA) you'll find support for many live templates that allow for code generation in the editor even for Apex. I don't know if this is available in VSCode since I don't use it.
    – Phil W
    Commented Mar 21, 2022 at 12:16
  • In case you're using VSCode you can take a look at the Apex extension, which has code-completion suggestions. Commented Mar 21, 2022 at 12:19
  • GitHub Copilot will also work with Apex: copilot.github.com
    – nbrown
    Commented Mar 21, 2022 at 13:06
  • 1
    Addendum: I watched the Lombok video and unfortunately there can't be any annotations like the ones used in Lombak in Apex because the code is compiled on Salesforce's servers. The only productivity options are typeahead, snippets, and templates, but within those, you have a lot of options for saving time.
    – nbrown
    Commented Mar 21, 2022 at 13:21

2 Answers 2

4

Illuminated Cloud (paid plugin for IntelliJ) has live templates for Apex and Lightning Web Components to help save time. Likewise, you can create your own live templates for IntelliJ if desired.

There's a video of the Apex Live Templates in action, but an example of a live template is that you can type sqblt + prompts for the object type to output the following where your cursor ends up in the query to define fields

for (List<SObjectType> results : [SELECT fields cursor FROM SObjectType]) {
    for (SObjectType result : results) {

    }
}

It also has Template-based source file creation - so you can quickly create apex unit test classes, a default apex class, etc with expected values and format. You can configure and/or extend them as you wish.


Otherwise, in VS Code you can achieve similar functionality to the above. The Salesforce Extension Pack comes with several code snippets to save yourself time.

You can see them defined here. Ex.

"Map from list of records": {
     "prefix": "mapfromrecordlist",
     "body": [
         "Map<Id, ${1:SObject}> ${2:variable} = new Map<Id, ${1}>(${3:recordList});"
     ],
     "description": "Map from List of records"
 },

While writing apex, you can select Insert Snippet from the Command Palette (Ctrl+Shift+P) to see available snippets for you. Once you know the prefix, you can then just use that.

enter image description here

You can also define your own User Snippets (notice log in above screenshot).

File --> Preference --> User Snippets --> Select "Apex"

In the apex.json file that opens, you can define a snippet.

Each snippet is defined under a snippet name and has a prefix, body and description.

  • The prefix is what is used to trigger the snippet
  • body will be expanded and inserted.
  • Possible variables are:
    • $1, $2 for tab stops,
    • $0 for the final cursor position
    • ${1:label}, ${2:another} for placeholders. Placeholders with the same ids are connected.

The example given in the file is below:

"Print to console": {
    "prefix": "log",
    "body": [
    "console.log('$1');",
        "$2"
     ],
     "description": "Log output to console"
}

Where typing log + tab will output console.log('cursor'); with your cursor focus in the message

If you don't feel like creating your own - you can search for extensions that provide it. A quick search (have not used these) shows several you can try:

And, in terms of class/file template creation, you can modify Custom Code Templates. The ones provided are listed here, but you can modify it if you expect certain things to exist when creating an Apex Unit Test, DefaultApexClass, etc.

1
  • Live Templates are da bomb - i couldn't live without them
    – cropredy
    Commented Mar 23, 2022 at 0:05
0

I am using JetForcer plugin for IntelliJ: https://plugins.jetbrains.com/plugin/9238-jetforcer--the-smartest-force-com-ide

I am quite happy with it!

I think you can use the IntelliJ snippets there and this should save you some time.

Then combine this with the SF APEX recipes and I think you would be satisfied: https://trailhead.salesforce.com/en/content/learn/projects/quick-start-explore-the-apex-recipes-sample-app

You must log in to answer this question.

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