Sub topic 11
Permissions, data quality, and size limits
Earlier chapters showed how to get data into a prompt template. This chapter covers what limits that data. Grounded output can go wrong in three ways. The running user may not be allowed to see a value. The value may be present but wrong, messy, or empty. Or there may be too much data for the prompt. This chapter assumes you have built a template with merge fields and at least one Flow or Apex resource, and that you know Salesforce field-level security, sharing, and Apex governor limits.
How the three limits fit together
A prompt template resolves in a fixed order. Each stage can shrink, distort, or drop the data you meant to send.
The model only sees what survives these stages. When the output is wrong, check the resolved prompt before you rewrite the instructions.
Permissions
Merge fields follow the running user
A merge field such as {!$Input:Case.Description} is resolved for the user who runs the template. If that user's profile or permission sets don't grant read access to the field, or sharing hides the record, the value is not inserted. The prompt reaches the model with a gap where the data should be.
Two users can run the same template on the same case and get different output, because their access differs. This is expected behavior. It is also why a template that works in your admin preview can fail for a support agent.
The same applies to related records. A template that uses {!$Input:Case.Account.Industry} needs read access to the Case, the Account, and the Industry field.
Check access at three levels:
- Object permissions: can the user read Case, Account, and Contact?
- Field-level security: can the user see each field the template references?
- Record access: does sharing give the user this specific record?
Besides these, the user needs permission to run the template itself. Salesforce provides permission sets for using Prompt Builder templates. Confirm the exact permission set names in your org's Setup, because they change between releases.
Flow and Apex can bypass what merge fields respect
Merge fields are conservative. Flow and Apex are not, unless you make them so.
- Flow: check the flow's run mode. A flow that runs in system context can read records and fields the user could not. Its output then flows into the prompt, and the person who sees the generated text may be someone who was never meant to see that data.
- Apex: a class declared
without sharing, or a query that usesWITH SYSTEM_MODE, ignores record sharing or field security. The class in the earlier chapter usedwith sharingandWITH USER_MODEfor this reason. Together they enforce sharing, object permissions, and field-level security for the running user.
What happens to the data after resolution
The resolved prompt passes through the Einstein Trust Layer before it reaches the model. Depending on how your org is configured, this layer can mask sensitive values, apply zero-data-retention terms with the model provider, and check responses for toxic content. Don't assume the Trust Layer replaces access control. It protects data in transit to the model, but it does not decide who may see the generated output. Permissions on the inputs still matter.
Also consider whether a field should be in a prompt at all. A user who can read a Social Security number field can technically ground a template on it. That does not make it a good idea. Leave out fields the task does not need.
Data quality
The model treats the prompt text as true. If a case description is wrong, the summary will faithfully repeat the error. Grounding fixes a lack of facts. It does not check the facts it inserts.
Empty values
An empty field produces an empty string, so the prompt contains a label with nothing after it:
Status:
Priority:
Description: A model may fill that gap with a plausible guess. Two defenses work together. First, tell the model what to do, as in the first chapter's template: "If a value above is empty, say it is not provided. Do not guess." Second, add an explicit fallback in the data source. The Apex example returns No open tasks. when the list is empty, so the model sees a clear statement and not blank space.
Noisy and inconsistent values
Common problems:
- Rich text and HTML. A rich text field inserts its markup along with its text. Tags use up space and can confuse the summary. Prefer a plain text field where one exists, or strip markup in Flow or Apex.
- Pasted content. Email threads and logs in a description field can contain signatures, disclaimers, and quoted replies that drown out the actual request.
- Free-text picklist substitutes. "High", "high", "P1", and "urgent" all mean the same thing to a person. A model usually handles this, but a template that branches on an exact value in instructions will not.
- Stale records. A contact's old job title or an account's out-of-date industry feeds into the output as current fact. Consider adding a date to the grounded data, such as the last modified date, so the model can say "as of".
- Duplicates. Two near-identical open tasks inflate the prompt and bias the summary.
Where to fix quality problems
Fix data at the cheapest layer that works:
- Source: validation rules, required fields, and picklists stop bad data from entering. This is the best fix and it helps every consumer of the data.
- Resource: a Flow or Apex resource can filter, sort, deduplicate, and format before inserting. This is the right place to strip markup or drop closed records.
- Instructions: the template text can tell the model how to treat gaps or conflicting values. This is the weakest layer, because the model still receives the bad data.
Size limits
A prompt has a finite size. The model has a context window, which is the maximum amount of text it can process in one request, covering the prompt and the response. The exact limit depends on the model you choose in Prompt Builder, so check the documentation for your model and release rather than relying on a remembered number.
Large grounding fails in less obvious ways than a hard error:
- Long text fields, such as a case description with a 30-page email chain, can take up most of the space.
- A Flow that returns every order a customer ever placed grows without bound.
- Long prompts raise latency, and they can cost more tokens. A token is a chunk of text, roughly a short word or word fragment.
- Even when a long prompt fits, a model can pay less attention to details buried in the middle.
Keep the inserted data bounded
Design every list-style resource with a cap, an order, and a format.
- Cap: always limit the number of rows, as the
LIMITclause does in a SOQL query, or a loop counter in Flow. - Order: sort so that the rows kept are the most useful ones, usually newest first or soonest due.
- Format: emit short lines, not full records. Include the two or three fields the instruction needs.
Apex governor limits also apply
An Apex resource is ordinary Apex, so governor limits apply. These include the SOQL query limit per transaction and the row limit per query. Salesforce can call an invocable method with a list of requests, so a method that queries inside its loop can hit the query limit. Write the method in bulk form: collect the IDs, query once, and group the results.
Here is the earlier example rewritten to be bulk-safe, size-bounded, and quality-aware:
public with sharing class CaseOpenTaskGrounding {
private static final Integer MAX_TASKS_PER_CASE = 10;
private static final Integer MAX_SUBJECT_LENGTH = 80;
public class Request {
@InvocableVariable(required=true)
public Case RelatedEntity;
}
public class Response {
@InvocableVariable
public String Prompt;
}
@InvocableMethod(
label='Get open tasks for case'
capabilityType='PromptTemplateType://einstein_gpt__fieldCompletion'
)
public static List<Response> getOpenTasks(List<Request> requests) {
Set<Id> caseIds = new Set<Id>();
for (Request req : requests) {
caseIds.add(req.RelatedEntity.Id);
}
Map<Id, List<String>> linesByCase = new Map<Id, List<String>>();
for (Task t : [
SELECT Subject, ActivityDate, WhatId
FROM Task
WHERE WhatId IN :caseIds AND IsClosed = false
WITH USER_MODE
ORDER BY ActivityDate ASC NULLS LAST
LIMIT 2000
]) {
List<String> lines = linesByCase.get(t.WhatId);
if (lines == null) {
lines = new List<String>();
linesByCase.put(t.WhatId, lines);
}
if (lines.size() < MAX_TASKS_PER_CASE) {
String subject = String.isBlank(t.Subject)
? '(no subject)'
: t.Subject.abbreviate(MAX_SUBJECT_LENGTH);
String due = t.ActivityDate == null
? 'no due date'
: 'due ' + String.valueOf(t.ActivityDate);
lines.add('- ' + subject + ' (' + due + ')');
}
}
List<Response> results = new List<Response>();
for (Request req : requests) {
List<String> lines = linesByCase.get(req.RelatedEntity.Id);
Response res = new Response();
res.Prompt = (lines == null || lines.isEmpty())
? 'No open tasks.'
: String.join(lines, '\n');
results.add(res);
}
return results;
}
}Each change maps to a limit from this chapter:
| Change | Limit it addresses |
|---|---|
| One query for all requests | SOQL query count governor limit |
| `LIMIT 2000` and a per-case cap of 10 | Row limits and prompt size |
| `ORDER BY ActivityDate` | Keeps the most urgent tasks when the cap cuts rows |
| `abbreviate(80)` on the subject | Prompt size from long free text |
| `(no subject)` and `no due date` | Empty values that would invite guessing |
| `WITH USER_MODE` and `with sharing` | Permissions: the running user's access |
Note that the global LIMIT 2000 is shared across all requests in one call. With many requests, some cases could lose their tasks to the cap. For typical prompt use, a single record per call keeps this a non-issue, but keep it in mind if the template is called in bulk.
Writing instructions that account for the limits
Your template text can state the constraints so the model handles them predictably:
You are a support assistant. Summarize the case below in two sentences.
Case subject: {!$Input:Case.Subject}
Description: {!$Input:Case.Description}
Open tasks:
{!$Flow:Get_Open_Tasks.Prompt}
Rules:
- Use only the information above.
- If a value is empty or says "not provided", say so. Do not guess.
- If the open task list is shown as "No open tasks.", do not invent tasks.The rules do not fix missing data. They stop the model from covering the gap.
Diagnosing a bad result
When a grounded response looks wrong, work through the pipeline in order:
| Symptom | Likely cause | Check |
|---|---|---|
| A field is missing for one user only | Field-level security or sharing | Run the preview as that user |
| Output cites data the user shouldn't see | Flow in system context or `without sharing` Apex | Review run mode and class declaration |
| Model invents values | Empty field with no fallback instruction | Look for blank labels in the resolved prompt |
| Output ignores key details | Prompt too long or too noisy | Check length of long text fields and list resources |
| Odd symbols in the text | Rich text markup | Strip markup in the resource |
| Apex error under load | Governor limit hit | Look for queries inside loops |
The preview from the previous chapter shows the resolved prompt. Use it as your first diagnostic step, and test with a user whose access matches your real audience, not only with an administrator.
Exercise
A support agent reports that the case summary template never mentions the account industry, but it works in your admin preview. The template uses {!$Input:Case.Account.Industry}. Give two likely causes and one way to confirm.
Show answer
Likely causes: the agent's profile or permission sets lack field-level read access to Account.Industry, or the agent lacks read access to the Account object or the specific Account record through sharing. The merge field resolves under the running user's access, so the value comes through empty. To confirm, preview the template while running as the agent, or use "login as" for that user, and check whether the Industry line is blank in the resolved prompt. Then compare the agent's field permissions against yours.
Exercise
A Flow resource returns all orders for a customer. Some customers have thousands of orders, and the summaries for them are slow and ignore recent activity. What three design changes would you make to the Flow's output?
Show answer
Cap the number of rows (for example, the 10 most recent). Sort so the kept rows are the most useful, such as newest first. Format each row as a short line with only the fields the instruction needs, such as order date, status, and total, instead of the full record. Optionally, return a clear fallback like "No recent orders." when the list is empty.
Exercise
Why is a "without sharing" Apex grounding class risky even when the template itself only displays a short summary?
Show answer
The class can read records the running user is not allowed to see, and the model may include facts from them in the generated text. That text can then be shown, saved, or sent to people who should not have the data. The Trust Layer does not enforce record access on who sees the output, so the class itself must enforce it with "with sharing" and USER_MODE queries.