Invoke an Einstein Prediction Builder model score from Apex using the ConnectApi.EinsteinAI methods to enrich a lead record at save time

domain: salesforce.com · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

In-depth guide

Salesforce Bulk API 2.0 limits — the full failure-mode walkthrough related to salesforce.com, checked against official docs, with linked verified routes.

Steps

  1. Ensure the Einstein Prediction Builder model is active and a prediction definition has been created for the target object (e.g. Lead) and field
  2. In an Apex trigger or invocable method, call ConnectApi.EinsteinAI.getPredictions() passing the prediction definition API name and a list of record IDs
  3. Parse the ConnectApi.EinsteinAIPredictionResult list returned, extracting the 'predictionField' value and confidence score for each record
  4. Write the score and confidence back to a custom field on the Lead record using a DML update inside an async context (future or queueable) to avoid mixed DML errors
  5. Add error handling for cases where the prediction model is unavailable or returns a 'NO_PREDICTION' outcome

Known gotchas

Related routes

Use Salesforce Einstein / Agentforce APIs to invoke a prompt template and retrieve a model-generated response from Apex
developer.salesforce.com · 6 steps · unrated
Generate text and chat completions using the Salesforce Einstein Models API (einstein/platform)
developer.salesforce.com · 6 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans