Sub topic 11
Permissions, Data Quality, and Size Limits
This chapter assumes you can build a prompt template with merge fields, a Flow resource, and an Apex resource, and that you know Salesforce permission sets, field-level security, and sharing. Grounded output is only as good as the data that reaches the model. Three things decide that: who is running the prompt, how clean the data is, and how much text fits.
Whose permissions apply
A grounded prompt resolves with the access of the user who runs it. If that user cannot see a field, the merge field does not return the value. The model never sees it, so it cannot use it.
This gives you two practical rules:
- Two users can run the same template on the same record and get different resolved prompts.
- A template that looks correct in your admin preview can fail for a rep with a narrower profile.
Check these layers when a value is missing:
- Object permission: can the user read the object?
- Field-level security: can the user read the field?
- Record access: does sharing give the user the record?
- Feature access: does the user have the permission set that allows the prompt template to run?
Flow and Apex run in their own context
Merge fields follow the running user's access. Flow and Apex need more care, because they can run in a context that is broader than the user's.
- A Flow can run in user context or system context. In system context it can read records the user cannot see. Use user context for grounding unless you have a reason not to.
- Apex declared
with sharingrespects record sharing, but it does not enforce field or object permissions by itself. The example in this tutorial addsWITH USER_MODEto the query, which enforces them as well.
Without that clause, an Apex resource can leak a field that the user is not allowed to read into the prompt, and from there into the response.
List<Task> tasks = [
SELECT Subject, ActivityDate
FROM Task
WHERE WhatId = :req.RelatedEntity.Id AND IsClosed = false
WITH USER_MODE
LIMIT 10
];Data quality
The model treats grounded values as facts. Blank fields, stale values, and free text full of noise all weaken the output.
| Problem | What happens | Fix |
|---|---|---|
| Empty field | The prompt contains a blank, and the model may invent a value | Tell the model to say "not provided" and not to guess |
| Stale value | The model repeats outdated status or contact details | Add a date field to the template, or filter by recency in Flow or Apex |
| Noisy text | Email signatures and pasted threads use up space and distract the model | Pull a shorter field, or trim the text in Flow or Apex |
| Duplicate records | Related lists repeat the same item | Deduplicate in the query |
The empty-field instruction from the first template is the cheapest defense:
If a value above is empty, say it is not provided. Do not guess.A Flow or Apex resource should return a clear fallback when it finds nothing. The Apex example returns No open tasks. for this reason. An empty string leaves the model with a gap in the prompt, and it may fill that gap with invented content.
Size limits
A prompt has a limited size, and the limit covers the whole resolved prompt: your instructions, every merge field, and all text returned by Flow and Apex. The exact limit depends on the model you select and on your org, so check the current Salesforce documentation instead of relying on a number from memory.
Long text fields such as Description, email bodies, and knowledge articles are the usual cause of overruns. Reduce size in these ways:
- Limit the rows. The Apex example uses
LIMIT 10. Use the same idea in a Flow Get Records element. - Select only the fields the task needs.
- Return short lines, such as
- Subject (due date), not full records. - Sort so the most useful rows come first, because anything cut off later is lost.
Size also affects cost and speed. A smaller, focused prompt is usually faster and often gives a better answer than one that includes everything.
A test routine
Use the preview from the previous chapter to check all three concerns before release:
- Preview with a record that has all fields filled in.
- Preview with a record that has empty fields, and confirm the prompt handles them.
- Preview with a record that has very long text, and check for truncation.
- Run the template as a user with a restricted profile, and compare the resolved prompt with the admin version.
Exercise
A sales manager previews a template and sees the account industry in the resolved prompt. A rep runs the same template on the same account, and the model says the industry is not provided. Name two likely causes and how you would check each.
Show answer
The rep may lack field-level security on Account Industry, or may lack access to the account record through sharing. Check by looking at the rep's permission sets for read access to the field, then confirm the rep can open the account record. Preview the template as the rep, or compare the resolved prompts, to confirm the value is missing before the model is called.
Exercise
Your Apex resource returns every open task for a case, and some cases now fail because the prompt is too long. Give two changes to the resource.
Show answer
Add a LIMIT clause to the query, and return short lines such as subject and due date instead of full task text. You can also sort by due date so the most urgent tasks are kept.