Effective Prompt Writing
A prompt is 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 Dev Assistant, the quality of your prompt determines the quality of the code that’s generated.
As with all LLM-based products, the technology behind Agentforce for Developers 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 tab. We recommend tools such as Apex PMD, Prettier (already in your Salesforce Extension Pack) and Salesforce Code Analyzer to ensure the quality of your generated code.
In addition to your instructions, the response generated by an AI depends on the context that your instruction is grounded with. The xGen-Code
model has been trained on Apex data and grounded with the context of Apex programming best practices. Additionally, for some users, we use RAG to gather context about SObjects in their Salesforce projects.
The xGen-Code
model uses this context from your project and prompt:
- Code that’s currently visible in the active file in your editor. The code in the entire file is used if the
Use entire file?
toggle isON
. - 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.
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 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 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 the SOQL uses security best practices like user-mode data operations. |
|
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. |
|
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. |
|
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. |
|
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. |
- 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. Ensure that the SOQL query is secure by adding WITH USER_MODE.
Use Dev Assistant to refine your requests, starting with general questions and moving on to more specific ones. For instance, you could begin by asking the assistant to create an Apex class named OpportunityController
with a method called getNumberofOpportunities
that takes an AccountId
as a parameter and returns the number of opportunities for that AccountId. Your prompt would look something like this:
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.
You could then follow up with a more specific request, such as asking the assistant to add error handling to the getNumberofOpportunities
method for cases where the AccountId
doesn’t exist. Your next exchange with Agentforce would be something like this:
Can you add error handling to the getNumberofOpportunities method for cases where the AccountId doesn’t exist?
Finally, you could ask the assistant to add a method to the OpportunityController
class that retrieves a list of Opportunity names for a given AccountId
. With each iteration, the assistant will be able to generate more refined and specific code.
The final prompt and response is:
Can you add a method to the OpportunityController class that retrieves a list of Opportunity names for a given AccountId? Please return the class with these two methods.