ContentVersion Agent Workflow (RunAgent)
How to run an agent against a Salesforce file (ContentVersion) via Flow and let it classify/label the linked parent record.
When this works
- Your Flow calls
RunAgentActionwithrecordIdset to aContentDocumentId (069…). - The
AIModel__cagent is configured forSObjectType__c = ContentDocumentand has a DML-capable skill/tool (for example,dml_sobjectsor a custominvoke_apexfunction) to write back to the parent record. - A business
ContentDocumentLinkexists to the target parent record; otherwise the “current record” will be the file itself. - Textract/text extraction for the ContentVersion has completed (cached text present); otherwise the API returns a “Give me a minute…” message and you must retry later.
What the model sees
RunAgentAction populates systemMessage with AIModel.getSystemPrompt(RunContext.Background), which includes:
# File ContextwithContentVersionId/ContentDocumentIdand ContentVersion field metadata.# Record Context (the record you should read/update)with the EffectiveRecordId/ObjectType, resolved by walking the most recent businessContentDocumentLink(falls back to the ContentDocument if no business links).- Optional full field tables for the parent record when
IncludeAllSObjectLabelsValues__corIncludeAllSObjectMetadata__cis enabled on the agent. - Merge tags (e.g.,
{{RecordID}},{{File.ContentVersionId}}) replaced with the effective values.
POSTAIResponse then builds final instructions as:
- Default document assistant prompt.
- The agent systemMessage above (with record + file context).
- The extracted document text appended inline.
Result: the model has both the parent record ID/type and the document text at the same time, so prompts like “read the file and update these fields” are actionable.
Flow wiring (inputs to RunAgentAction)
recordId: required, must be the ContentDocument Id of the file you want processed.agentNameorsystemAgentId: required agent lookup (AIModel__c).userPrompt: optional kickoff instruction (e.g., “Classify this invoice and set Amount__c/Invoice_Date__c.”).runAsync: keeptruefor background agents (default).contentVersionIdinput is not used; the action always uses the latest published version of the provided ContentDocument.
Preconditions & safeguards
- Ensure the agent has permission to update the target object (via the DML tool/skill and auth context). Background run uses SYSTEM_MODE for config objects; business object queries honor ExecPolicy.
- Set
IncludeAllSObjectLabelsValues__c(and/orIncludeAllSObjectMetadata__c) if you want all parent fields exposed in the prompt. - Verify a business CDL exists; without it the EffectiveRecordId stays on the ContentDocument, so no parent fields are available to update.
- Extraction must exist: Textract/DialogueCache needs cached text for the ContentVersion; otherwise the API replies with the waiting message and does not run tools.
Operational notes
- The action always targets the latest published ContentVersion of the ContentDocument passed in
recordId. - Parent resolution picks the most recent business-linked
ContentDocumentLink(SystemModstamporder). If multiple links exist, update the CDL you want to take precedence. - If the agent should update multiple records, add a custom skill to locate related records; only one EffectiveRecordId is injected automatically.
- Use transcripts/events to confirm the run:
response.messageholds the assistant completion; platform events capturerecordIdandcontentVersionId.
Troubleshooting
- Prompt shows ContentDocument as record: no qualifying CDL; add a link to the target record and retry.
- Assistant says to wait: text extraction not ready; rerun after Textract finishes.
- Fields not visible in prompt: enable
IncludeAllSObjectLabelsValues__c/IncludeAllSObjectMetadata__con the agent; ensure the running user/ExecPolicy has access. - Agent doesn’t write back: confirm a DML-capable tool is active and permitted for the parent object; check RunContext (Background) if your tool choices differ.