Sub topic 11
Using prompt templates as actions
This chapter shows how to turn a Prompt Builder template into an action that an agent can call. It assumes you have a working agent with at least one topic, as built in the earlier chapters. It also assumes you have read the Einstein Trust Layer chapter and can open Setup in Lightning Experience. You don't need Apex for this chapter.
What a prompt template action is
An action is something an agent can do. Standard actions query records or summarize them. Custom Apex actions run your code. A prompt template action is a third kind. It sends a prompt that you wrote to the large language model (LLM), fills in the prompt with real data from your org, and returns the generated text to the agent.
Use a prompt template action when you want the same wording and the same data to produce a consistent kind of output. Examples are a case summary, a follow-up email, or a short account briefing. Without a template, the agent composes this text from its general instructions, and the result varies more from run to run.
Prompt Builder is the Setup tool where you create and manage these templates. A template is reusable. The same template can power a field on a record page, an email draft in the Sales app, and an agent action.
Template types and which one fits agents
Prompt Builder offers several template types.
| Type | Typical use | Usable as an agent action |
|---|---|---|
| Flex | General prompts with inputs you define | Yes |
| Sales Email | Draft emails from a contact or lead | Yes, in orgs where the type is available |
| Field Generation | Fill a field on a record | No, it runs from the record page |
| Record Summary | Summarize a record on its page | No, it runs from the record page |
Flex is the type to learn first. It lets you declare your own inputs, such as an Account record or a block of text, and refer to them anywhere in the prompt. Other types are tied to a specific record page, so an agent cannot call them with its own inputs.
The available types and their names change between releases. If a type is missing in your org, open the template creation dialog and read the type descriptions there.
How a prompt template action runs
The agent chooses an action based on the topic's instructions and the action's description. It then supplies the inputs. The platform resolves the template, grounds it with your data, applies Trust Layer protections, and calls the LLM. The generated text goes back to the agent, which includes it in its reply.
Two points follow from this flow. The template reads data with the permissions of the running user, so a user who cannot see a field will not get that field in the prompt. And the Trust Layer processes the prompt as it would for any other generative AI request. Masking and toxicity checks apply, and the interaction can be audited when that feature is enabled.
Prerequisites in Setup
Before you create a template, confirm three things:
- Einstein Generative AI is on. You enabled this in the "Turning on Agentforce in Setup" chapter.
- Prompt Builder is available. In Setup, search for Prompt Builder. If the page is missing, generative AI is not enabled yet.
- Your admin user has the permission set that allows managing prompt templates, usually named Prompt Template Manager. Users who only run templates need the Prompt Template User permission set. Assign these the same way you assigned the Agentforce permission sets earlier.
Creating a Flex template
- In Setup, open Prompt Builder and choose New Prompt Template.
- Select Flex as the type.
- Enter a name such as
Account Case Briefing. The API name is generated from it. Keep the name short, because it appears in the action list. - Add an input. Choose the Record input type, set the object to Account, and name the input
Account. - Write the prompt body in the large text area.
- Use Preview to test against a real record, then save and activate a version.
Here is a prompt body that uses the input. The merge fields use the {!$Input:...} syntax. You can type them, or insert them with the resource picker, which avoids spelling mistakes.
You are a support operations assistant. Write a briefing about this account for a support agent who is about to take a call.
Account name: {!$Input:Account.Name}
Industry: {!$Input:Account.Industry}
Account owner: {!$Input:Account.Owner.Name}
Instructions:
- Write at most four sentences.
- Mention the account name once, in the first sentence.
- State the industry and who owns the account.
- Do not invent details. If a value above is blank, say it is not recorded.
- Use a neutral, professional tone.The last three instructions matter. A prompt without a rule for blank values will often fill the gap with plausible but invented text. An explicit rule is the cheapest way to reduce that.
Adding more data
A prompt is only as useful as the data grounding it. The resource picker lets you insert fields from the input record and from related records. For related lists, check what the picker offers for your object. A Flex template can also call a Flow or an Apex class to fetch data that a merge field cannot reach, such as an aggregate. Keep the number of resources small, because every extra resource adds to the prompt size and to the time the action takes.
Versions and activation
A template has versions. You edit a draft version and activate it when ready. The agent uses the active version, so an edit to a draft does not change agent behavior until you activate it. This lets you test changes safely. When you activate a new version, the previous one becomes inactive but is kept.
Run Preview with a record that has blank fields as well as one that is complete. The blank case is where most prompt problems show up.
Adding the template to an agent
- Open your agent in Agentforce Builder.
- Select the topic that should own the action. Choose a topic whose scope matches the template. A case briefing belongs with case or account questions, not with an unrelated topic.
- Choose New Action, then pick the reference action type Prompt Template.
- Select your
Account Case Briefingtemplate. - Edit the action's name and instructions. The action instructions are what the agent reads to decide whether to call it.
- Review the inputs. For each input, decide whether the agent should collect it from the user or take it from the conversation context. Mark an input as required if the action cannot run without it.
- Save the action.
Then add a numbered line to the topic instructions that says when to use it. Mirror the style of the earlier sample topic:
Topic: Account Briefings
Description: Give support staff a short briefing about a customer account.
Instructions:
1. Ask for the account name if the user has not given one.
2. Use the Account Case Briefing action to generate the briefing.
3. Return the briefing text as written. Do not add facts that are not in it.
4. If the user asks about anything other than account briefings, say you cannot help with that.Moving the template with the CLI
Prompt templates are metadata, so you can keep them in source control. The metadata type is GenAiPromptTemplate. This retrieves the template into your project and deploys it to another org. Replace the name with your template's API name.
sf project retrieve start --metadata GenAiPromptTemplate:Account_Case_Briefing --target-org agentforce-dev
sf project deploy start --source-dir force-app --target-org agentforce-devDeploying to a different org requires that the target org has generative AI enabled and that any referenced Flows or Apex classes are deployed first.
Testing and common errors
Test the template twice, first on its own in Prompt Builder and then through the agent in the conversation preview. A template that works alone can still fail through the agent because the agent passes the wrong input.
| Symptom | Likely cause | Fix |
|---|---|---|
| The action never appears in the preview plan | The topic instructions do not mention it, or the description is vague | Add a numbered instruction naming the action and rewrite the description |
| The agent asks the user for the account ID | The input is a record and the agent cannot resolve a name to it | Add instructions to look up the account first, or use a standard query action before the template |
| The output contains blank or "null" values | A merge field points at an empty field, or the user lacks field access | Add a blank-value rule to the prompt and check field permissions |
| Template not in the action picker | No active version | Activate a version in Prompt Builder |
| The output ignores your rules | The prompt is too long or its rules contradict each other | Shorten it, put the rules in a list, and re-test |
| Output differs after an edit | A new version was activated | Check which version is active |
In the conversation preview, open the plan or trace view for a turn. It shows which action was chosen and which inputs were passed, so you can see whether a failure came from topic selection, input values, or the prompt itself.
Template or Apex action?
Choose a prompt template when the output is generated text and your main work is wording and grounding. Choose an Apex action when the result is a number, a record change, or any exact value, as in the open case count example. A common pattern combines them. An Apex action fetches an exact value, and a template turns it into a sentence. Always prefer code for facts that must be exact, because an LLM can misstate a number.
Exercise
A user asks your agent for an account briefing. The agent never calls your prompt template action, though the template previews correctly in Prompt Builder. Name two places to check, and say what to change in each.
Show answer
First, check the topic instructions. Add a numbered instruction that tells the agent to use the template action for briefing requests. Second, check the action description. Rewrite it so it states what the action produces and what input it needs. You should also confirm the template has an active version, since an action cannot use a template with no active version.
Exercise
Why does the prompt body include the rule "If a value above is blank, say it is not recorded"?
Show answer
Without it, the LLM may fill a blank field with invented but plausible text. The rule gives the model a safe way to handle missing data, which reduces made-up details in the output.