Generate Apex Code
Use the Einstein for Developers side bar to write a question or an instruction that describes the task for which you’d like a receive an Apex code suggestion and press Ask. Copy the code suggestion you receive and paste it into an Apex file to use it as a “starter” code.
Generate Apex Code Using a Command Palette Command
To quickly access Einstein for Developers from inside an Apex file in the VS Code Editor:
- Open an existing Apex (
.cls) file, or create one from the command palette by running the SFDX: Create Apex Class command. You can also use the Org Browser to retrieve an Apex class from your org.
- Place your cursor on the line in the file where you want the generated code to be placed.
- From the Command Palette, run Einstein: Generate Code.
- For your query, enter text that describes the code that you want to generate. For example, “Write a method that takes an account as a parameter and returns all contacts associated with that account.”
- Accept, Try Again, or Clear the code that Einstein generates.
Use our example prompts to exercise your code generation muscle and learn how to get the most out of the generative AI tool.
Note: You can also access the Einstein: Generate Code command through hotkeys. Keyboard shortcuts are the following across environments:
- macOS - Cmd+R
- Windows & Linux - Ctrl+R
You may also customize these shortcuts. See Keyboard Shortcuts editor for more info.