Sub topic 11
Using prompt templates as actions
This chapter assumes you have a working agent with at least one topic, as built in the earlier chapters. It also assumes you have the Prompt Builder permission sets described in the permissions chapter. Prompt templates are the lowest-code way to give an agent a custom action. You write the action in plain language and don't need Apex.
What a prompt template is
A prompt template is a reusable set of instructions for a large language model (LLM), written in Prompt Builder. It contains fixed text and merge fields. Merge fields are placeholders that Salesforce fills with real record data when the template runs. This filling-in is called grounding.
Prompt Builder offers several template types. The one that works as an agent action is the Flex template. You choose its inputs yourself, such as an Account record, so the agent can pass in whatever the user is discussing.
Why use a template instead of Apex
The two approaches fit different jobs.
| Need | Better choice |
|---|---|
| Look up or calculate a value, such as a count | Apex invocable method |
| Write or summarize text from record data | Prompt template |
| Change data or call an external system | Apex or Flow |
A template always produces generated text. If you need an exact number or a data change, use an Apex action instead. Templates also pass through the Einstein Trust Layer, so masking and logging rules apply to them as they do to the agent's own prompts.
Build a template
- In Setup, search for Prompt Builder and open it.
- Click New Prompt Template and choose Flex.
- Name it
Account Summary. Add an input of type Account (a record) and name itAccount. - Write the prompt, using the merge fields for that input.
- Click Preview, choose a real account, and read the response.
- Save and activate the template.
Here is a sample prompt body:
You are a support assistant. Write a three-sentence summary of this account
for a service agent who is about to take a call.
Account name: {!$Input:Account.Name}
Industry: {!$Input:Account.Industry}
Annual revenue: {!$Input:Account.AnnualRevenue}
Mention only facts listed above. If a field is empty, leave it out.
Do not invent details.The last two lines matter. They are the template's version of topic instructions, and they keep the model from filling gaps with guesses.
Attach it to the agent
- Open your agent in Agentforce Builder and select the topic that should use the template.
- Choose to add an action, and pick the Prompt Template action type.
- Select
Account Summaryfrom the list. Only activated templates appear. - Give the action a clear name and description. The agent reads these to decide when to call it.
- Add a line to the topic instructions, for example: "When the user asks for an account overview, use the Account Summary action."
Common errors
- The template is missing from the action list. It is probably still a draft. Activate it in Prompt Builder.
- The agent never calls the action. The action description or topic instructions don't match the way users phrase the request. Reword them and test again.
- The summary has blank or odd values. Either the running user lacks field access, or the record has no data in that field. Check field-level security and the record itself.
- The agent can't find the account. The action needs an Account input. Add an instruction to ask for the account first, as in the Apex action's topic.
Exercise
Your agent must report the exact number of open cases for an account. Should you use a prompt template action or the Apex invocable action? Why?
Show answer
Use the Apex invocable action. A prompt template produces LLM-generated text, so it can't guarantee an exact count. The Apex method runs a SOQL aggregate query and returns the real number. A prompt template suits tasks such as summarizing an account in prose.