While building my project, I came across a use case where I needed to capture AI-generated data using Flow and store it directly in Salesforce object fields. The goal was to store this data for later use.
Although Flow is powerful for automations, AI outputs are not formatted in a way that Flow can reliably use. This challenge led me to explore how Apex and Flow could work together to safely process AI outputs before storing them in Salesforce records.
Why a Parser is Needed
Even when AI can be instructed to return structured JSON, Flow can struggle with:
Nested or inconsistent data
Extracting specific values reliably
Without a parser, automation can fail or store incomplete data. An Apex parser safely extracts the required fields and makes them available to Flow without modifying the AI-generated values.
Workflow: Step by Step
Here’s how I handled the use case in my project:
1. Generate AI Output Using Prompt Template
- To make parsing simple, it’s important to instruct the AI to return structured JSON, for example:
{
"score": 85,
"confidenceLevel": "high"
}
Clearly specify the fields you need and which values are optional.
In Flow, I used the Prompt Template action to get the generated AI output.
💡 Tip: Effective prompts generate predictable JSON, making it much easier for the parser to extract the values accurately.
2. Capture AI Output in Flow
- Flow stores the AI response in a variable, ready to be passed to Apex for parsing.
3. Call Apex Parser from Flow
Flow sends the AI output variable to an Apex action.
The parser safely extracts the required fields and handles any missing or optional data.
Understanding the Apex Parser
Here’s the core of the parser:
Map<String, Object> data = (Map<String, Object>) JSON.deserializeUntyped(aiJson);
Decimal score = data.containsKey('score')
? Decimal.valueOf(String.valueOf(data.get('score')))
: null;
String confidence = data.containsKey('confidenceLevel')
? String.valueOf(data.get('confidenceLevel'))
: null;
JSON.deserializeUntyped converts the JSON string into a map.
The map lets us safely check for missing fields.
Values are converted to the correct type, or set to null if missing.
The parser does not modify the AI-generated values; it only extracts them safely for Flow.
Key points for calling Apex from Flow
* @InvocableMethod lets Flow call the Apex parser
* @InvocableVariable allows data to pass in and out.
4. Flow Receives Parsed Data and Updates Records
Flow receives the extracted fields as variables.
These variables are mapped directly to Salesforce object fields.
Flow can now create or update records safely, without worrying about missing or malformed AI output.
Best Practices
Start by parsing a few key fields before handling everything.
Keep a log of raw AI outputs for debugging purposes.
Test the AI prompts and parser together to ensure consistent and predictable results.
About Me
I’m a curious Salesforce Developer who loves turning complex ideas into simple, practical solutions — and sharing what I learn while building projects.
Top comments (0)