Snippet: From Free Text to Your Salesforce Data Model

Salesforce is great at handling structured data. But most business input doesn’t arrive neatly structured. It comes in through emails, call notes, or chat transcripts. Turning that free text into usable records is hard. That’s where the Models API and a simple UI layer can help.

This post walks through how to map unstructured text to your org’s specific objects and fields, using three main components: Apex services, the Models API, and a Lightning Web Component (LWC).

Illustration of unstructured text transforming into structured Salesforce objects through AI.
Converting raw text, such as an email, into Salesforce objects with fields and relationships.

The Goal: Text-to-Record, Org-Aware

The idea is simple. Take any piece of free text—like a support email or sales rep’s notes—and turn it into a structured Salesforce data model. That means identifying the right objects, fields, and relationships, then extracting the actual values.

For example:

  • An email with a deal request should become an Opportunity with an Account, Contact, and Quote.
  • A support call summary should become a Case with an Entitlement.

The tool must also be org-aware. It can’t just rely on standard Salesforce objects. It should know your custom objects and fields without hardcoding them.

How It Works: Three Components

SfMetaCatalogService (Apex)

This Apex class scans your org’s schema. It builds a compact catalog of objects, fields, and relationships while respecting user permissions. That catalog is injected into the model prompt so the AI understands your exact org setup.

DataModelResolverService (Apex)

This service sends both the schema catalog and the user’s text to the Models API. A system prompt guides the AI to act as a Salesforce data resolver. It responds with structured JSON, including the field values it extracted.

textToModel (LWC)

This Lightning Web Component provides the UI. It gives users a text box, a button, and a preview panel. When a user submits text, the LWC calls the Apex controller, parses the JSON, and displays the proposed data model. The AI model is configurable, so you can swap in different available models.

Why This Approach Works

  • Dynamic: Because the org schema is scanned live, no hardcoded updates are needed when objects change.
  • Handles complexity: The model can resolve relationships across multiple levels—parents, children, siblings.
  • Extracts data: It doesn’t just map Opportunity.Amount; it also pulls out $125,000 from the text.
  • Safe: Output is JSON only. No records are created until a user validates.

Examples

Here are a few prompts to show what it can handle.

Example 1: Explicit Sales Request

Input:
“Create an Opportunity named ‘ACME Q3 Expansion’ (close date 2025-09-15, stage Proposal/Price Quote, amount $125,000) for Account ACME Corp. Add primary Contact Dana Ruiz (dana@acme.com, 415-555-0199). Generate a Quote linked to the Opportunity with QuoteLineItem records for 30 × Widget A and 10 × Widget B.”

Result:
The model identifies and extracts records for five related objects—Opportunity, Account, Contact, Quote, and Quote Line Items.

Salesforce Text-to-Model screen showing an Opportunity, Quote, and related records generated from a sales request.
The system extracts Opportunity, Quote, and Quote Line Items from a sales request and maps them to Salesforce records.

Example 2: Implicit Service Request

Input:
“Someone from ACME called in yesterday. Their support agreement says 4-hour response, but they’ve already waited 6. They want a callback ASAP from their account rep.”

Result:
The model infers the right records even though they aren’t explicitly named. It maps to Case and Account.

Salesforce Text-to-Model screen showing Case and Entitlement records generated from a service request.
The system infers Case and Account records from an unstructured customer support request.

Considerations

  • Hallucinations and Accuracy: The model can misinterpret fields or invent values. Always review before creating records.
  • Token Limits: In very large orgs, schema catalogs may exceed limits. Tune MAX_OBJECTS and MAX_FIELDS in the Apex service.
  • Data Security: Input text may include PII. Ensure your Models API setup complies with company privacy policies.
  • Cost: Processing high volumes of text isn’t free. Monitor usage to avoid surprise bills.

Potential Next Steps

  • Automated Record Creation: Add a “Confirm & Create Records” button to move beyond preview.
  • Integration with Salesforce Flow: Wrap the resolver in an invocable action so Flows can generate records directly.
  • Disambiguation in UI: Prompt the user when text is unclear (e.g., multiple matching Contacts).
  • Copilot for Agents: Embed the component in Service Console to pre-fill Case details while an agent types notes.

References

Source Code: Text-to-Data-Model on GitHub

Salesforce Documentation: Models API Developer Guide

AI-Ready Apex Trigger Framework – how to enforce CRUD/FLS with metadata-driven design.

The Takeaway

Turning free text into Salesforce records is no longer a dream. By combining org-aware schema scanning, the Models API, and a lightweight UI, you can build a reliable intake layer for both sales and service. The result: less manual entry, better data quality, and a faster path from messy text to actionable records.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top