---
schema_version: "1"
id: ff5ecff94e29445890c4f82f3cc836310833667b29fe8f7a13ae0fd8b77f0f15
title: Effective Prompt Writing
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/einsteinfordevs/guides/files/einstein-example-prompts.md
canonical_url: https://developer.salesforce.com/docs/platform/einstein-for-devs/guide/einstein-example-prompts.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: a0f28f31f5921444ce7ddd22b036bad4f5f3c6246270df69752b655013f03760
products:
  - Agentforce Vibes Developer Guide (Deprecated)
  - salesforcedx
---
# Effective Prompt Writing

A prompt is a natural language text that you craft to communicate with an AI. When you ask Agentforce a question, you’re sending a prompt. The better your prompt, the better Agentforce's response is. That’s why it’s important to use prompt engineering to craft your prompt carefully. After you receive a response for your initial prompt, you can make a follow-up request to receive a new, more accurate response. Refine your prompt until you get the response you need. For Agentforce, the quality of your prompt determines the quality of the code that’s generated.

## About Generated Code Quality

As with all LLM-based products, the technology behind Agentforce Vibes is still new. The generated output often requires editing to be customized for your individual project. Some responses aren't optimal.

We strongly encourage you to let us know when you see examples of less optimal code, so that we can continue to improve generated responses. We encourage you to post your feedback using the [Issues](https://github.com/forcedotcom/Einstein-GPT-for-Developers/issues) tab.
We recommend tools such as [Apex PMD](https://marketplace.visualstudio.com/items?itemName=chuckjonas.apex-pmd), [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) (already in your Salesforce Extension Pack) and [Salesforce Code Analyzer](https://github.com/forcedotcom/code-analyzer) to ensure the quality of your generated code.

## Context and Grounding

In addition to your instructions, the response generated by an AI depends on the context that your instruction is grounded with.

The `Core` model uses this context from your project and prompt:

- Custom objects in the schema that are mentioned in the object\_\_c format. Schema information takes precedence over Apex code in the current file.
- Custom metadata that is a part of a prompt and uses the \_\_mdt suffix. The metadata added to a prompt in this way is automatically included in the context.

## Prompt Writing Tips

We’re listing some tips for prompt writing and also providing you with some examples that help you craft your prompts. Use these tips to help ground your prompts further with org context awareness to generate a high-quality response. An effective prompt consists of these four parts. The relevant part is highlighted in the example:
|Part of Prompt | Description | Example |
| --- | --- | --- |
|Action |The task you want the AI to accomplish. |**Create an Apex batch class named `OpenPositionMatcher`** |
|Context details |Information about the context that you want the AI to use. This includes important context such as org metadata and awareness about the presence of Salesforce objects and fields in your org. Run **SFDX: Refresh SObjects Definitions** to retrieve org metadata into your project. You can then add Salesforce Objects to your existing query. |Create an Apex batch class named `OpenPositionMatcher` **that processes `Position__c` records created within the past 15 days. The class should match these open positions with suitable candidates based on their skills**. |
|Best practices |Details about best practices you want the AI to follow such as security and formatting best practices. |Create an Apex batch class named `OpenPositionMatcher` that processes custom objects `Position__c` records created within the past 15 days. The class aims to match these open positions with suitable candidates based on their skills. **Order the results by candidate names in descending order. Make sure the generated code follows security best practices. Separate operators with a new line**. |
|Additional context |Any additional information that you want to provide. |Create an Apex batch class named `OpenPositionMatcher` that processes custom objects `Position__c` records created within the past 15 days. The class should match these open positions with suitable candidates based on their skills. Order the results by candidate names in descending order. Make sure the generated code follows security best practices. Separate operators with a new line. **The class is used in an LWC component**. |

## Example Prompts and Context Descriptions

| Example Prompt                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | Context Description                                                                                                                                                                    |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Create an Apex class named `OpportunityController`. Create a method called `getNumberofOpportunities`. The method takes `AccountId` as a parameter and returns the number of opportunities for that `AccountId`. I’m working on an LWC component. Make sure that the SOQL uses security best practices like user-mode data operations.                                                                                                                                                         | <ul><li> Specific mention of an LWC component.</li> <li> Specific mention of security best practices</li></ul>                                                                         |
| Write an Apex class called `VehiclePartInventoryClass` for the custom object `Vehicle__c`, which has a many-to-many relationship with the `Parts__c` custom object. The class includes a method called `getVehiclesByParts` that accepts a Set of Strings. This method retrieves all the vehicle records associated with each part and returns a `Map<String, List<Vehicle__c>>`, where the key is the part name, and the values are lists of vehicles that have the specified part installed. | <ul><li> Specify custom object names including `__c`.</li><li> Specify method input and output types and any logic that you wish to see in the method implementation.</li></ul>        |
| Create an Apex method that takes a `settingName` as input and returns the value of the first record in `Setting__mdt` where `QualifiedApiName` matches the `settingName`. Return null if no records are found.                                                                                                                                                                                                                                                                                 | <ul><li> Specify any custom metadata.</li></ul>                                                                                                                                        |
| Write an Apex method that takes an Id as an argument. It compares the input Id with the value returned by the `getStagingOrgId()` method, and returns a boolean based on whether they match.                                                                                                                                                                                                                                                                                                   | <ul><li>Specify the relevant method name in the current file.</li><li>Specify method input and output types and any logic that you wish to see in the method implementation.</li></ul> |
| Create an Apex method that takes `settingName` as input and returns the value of the first record in `Setting__mdt` where `QualifiedApiName` matches `settingName`; return null if no records are found.                                                                                                                                                                                                                                                                                       |

## Some Sample Prompts

- Write an Apex method that accepts a list of sObjects and a field name and returns a set of non-null field values.
- Create an @InvocableMethod that serializes an input list of SObjects and returns JSON.
- Write an Apex method that accepts product name as input and retrieves order items filtered on product name.
- Write an Apex trigger to add the number of contacts associated with an account. Make sure that the SOQL query is secure by adding WITH USER_MODE.
