Einstein for Developers (Beta) is an AI coding assistant for Salesforce-specific languages. It empowers Salesforce Developers by automatically translating natural language instructions called prompts into Apex code. The key to accurate code generation lies in properly structuring prompts; well-framed prompts produce more robust code output. In this post, we provide tips to help you write effective prompts to get the most out of Einstein for Developers.

Prompt engineering with Einstein for Developers

Einstein for Developers uses Salesforce’s secure, custom model CodeGen2.5 that’s fine-tuned on Apex data. The prompting techniques required to get quality output differ from those required for general-purpose large language models (LLMs) like OpenAI’s GPT.

To extract relevant and accurate code from the model used for Einstein for Developers, you will need to do two important things:

  • Ground your prompt with your org metadata (schema and custom metadata)
  • Provide additional context details to the prompt about your project.

Grounding the prompt with org metadata

To pass all the schema information about your org from your project to the model, you’ll need to follow these steps:

  • Run the command SFDX: Refresh SObject Definitions via the command palette menu in your VS Code/Code Builder. This command ensures that your org schema details are stored in your local project folder. This information is passed on to the CodeGen2.5 model along with your prompt input.

Note: This is only needed if you have turned off the setting Enable-sobject-refresh-on-startup in your Salesforce Extensions settings as shown in the screenshot below.

Enable-sobject-refresh-on-startup setting to enable/disable automatic refresh of sObject definitions on extension activation

  • Include the API name of object with __c or custom metadata __mdt relevant to your prompt input. This will help the model recognize the object and automatically infer the fields it needs to work with.

The screenshot below displays the command in the Salesforce Extensions command palette menu that refreshes object definitions. Be sure to execute this command so that the model has up-to-date information on your org’s schema details.

Command to refresh object definitions

Refreshing the object definitions allows the model to access the latest custom and standard objects, fields, and relationships in your org. Keeping the schema details current enables the model to generate more accurate and functional Apex code tailored to your unique org configuration.

Example use case

To better understand the concept, let’s consider an example use case. Let’s assume we’re working with one of our sample applications, Dreamhouse. This application has a custom object property__c.

Suppose you need to generate an Apex class called PropertyService that includes a method named getHousesByCity. This method should retrieve all property__c records filtered by city. To accomplish this, you can follow the prompt below:

“Write a class named PropertyService and create a method called getHousesByCity. The method should take the city as a parameter and display a list of all property__c records filtered by the city.”

One of the responses I received for the above prompt is shown below. Note that responses do vary a bit each time, so you could try submitting your prompt a couple of times to see other responses and select one that you feel is most relevant.

Notice that the model included relevant fields in the output response without requiring an explicit mention in the prompt by simply providing the object name. The same applies if you want to include custom metadata __mdt.

It is important to note that you can get better responses by specifying the method name and the parameters. Also, you can optionally add return types to the Apex method to improve accuracy.

Context details

Providing enough context and details about your requirements is essential for the prompts to be effective. Let’s look at an example to better understand this. We’ll enhance the previous example prompt, but now add more context details about using this Apex method in LWC. Also, we want to ensure that the code follows SOQL security best practices.

To accomplish the above, our enhanced prompt is as follows:

“Write a class named PropertyService and create a method called getHousesByCity. The method getHousesByCity should take the city as a parameter and display a list of all Property__c records filtered by the city. The class is used in LWC. Make sure the SOQL uses security best practices like user mode data operations.”

One of the responses I received for the above prompt is shown below.

Note that the code now has:

  • @AuraEnabled annotation required for invoking the Apex from the LWC
  • WITH USER_MODE to make sure that the SOQL runs in the user mode

Below are some examples of text that I have found effective to add to the input prompt to help the model with additional context details for some common Apex use cases.

Example text to add to prompt to provide context Explanation
Write a batch Apex class Tells model to generate Batch Apex code
I am working on a Lightning Web Component (LWC) Tells model to add necessary @AuraEnabled annotation
Follow SOQL security best practices Tells model to write SOQL using user mode for security
Create an @InvocableMethod Indicates method will be used in Flows. It ensures the return types and input parameters are using Lists as expected by Flows
Serialize the response into a JSON object Tells model to use appropriate serialization method
Write a trigger delegating logic to an Apex handler class Writes trigger invoking separate Apex handler class
Generate an Apex class to deserialize the given JSON Generates Apex class to deserialize JSON to Apex objects
Generate a custom REST API endpoint in Apex Tells model to expose Apex class as REST service
Generate Apex to make an HTTP callout to an external system Provides boilerplate for making outbound HTTP callouts

Implicit context

In addition to the context details that you manually provide via your prompt, Einstein for Developers automatically infers relevant context from your open Apex files. For example, Einstein for Developers detects class names, methods, and code for files that are active in the editor when you execute the prompt. This allows the model to understand the project you are working on and tailor its output accordingly.

Below is an illustration of how we form the final prompt sent to the CodeGen2.5 model.

Diagram showing how we construct the final prompt sent to our secure model


For more examples, check out the official documentation on effective prompt writing. I also covered prompt writing techniques in-depth in a hands-on codeLive session, which you can access on demand.

This is just the beginning for Einstein for Developers. We are working on adding capabilities like referencing existing Apex classes in prompts for context. In the future, we will implement Retrieval Augment Generation (RAG). This will allow the model to infer your orgs schema, metadata, and existing classes without you having to specify these details in your prompts.

Finally, something to note, as with all large language models, the technology powering Einstein for Developers is still evolving. The generated code may require editing to customize it for your projects. Some responses will be less optimal than others. We welcome your feedback on the code output! Please share examples and suggestions via the Issues and Discussions tabs in the Einstein for Developers GitHub repository.

We hope the tips covered in this blog help you write better prompts to unlock the full potential of Einstein for Developers.

Happy prompting!

Additional resources

About the author

Mohith Shrivastava is a Developer Advocate at Salesforce with a decade of experience building enterprise-scale products on the Salesforce Platform. He is presently focusing on Salesforce Developer Tools, Apex, and Lightning Web Components at Salesforce. Mohith is currently among the lead contributors on Salesforce Stack Exchange, a developer forum where Salesforce Developers can ask questions and share knowledge. You can follow him on X (Formerly Twitter) and LinkedIn.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS