Saturday, January 3, 2026

Add Apex Merge Fields to a Field Generation Prompt Template

Salesforce allows you to make AI responses smarter by using Apex merge fields in a Field Generation Prompt Template. This helps Einstein Generative AI use real-time data from your org when creating AI-generated content.

Let’s understand this with a simple example.

Business Use Case

Imagine your sales team wants to see a summary of all open cases for a customer before making a sales call. Instead of checking multiple records, Salesforce AI can automatically generate this summary and store it on the Account record.

To do this, we use:

A Field Generation Prompt Template

An Apex class to fetch open case data

Using Apex with Prompt Templates

An Apex class can be written to:

Accept an Account as input

Find all open Cases related to that Account

Prepare the case details as text

Send this text to the AI model as part of the prompt

This Apex class is exposed to Prompt Builder using the @InvocableMethod annotation. Once exposed, it can be selected as a resource inside a prompt template.

public class OpenCasesPrompt {
    @InvocableMethod(label='Open Cases'
        description='Find Cases for an Account'
        CapabilityType='PromptTemplateType://einstein_gpt__fieldCompletion')
    public static List<Response> getCasesPrompt(List<Request> requests) {
        // Validate the expected number of requests as an input
        if (requests.size() != 1)
          throw new ListException('The requests list must contain one entry only');
        Account a = requests[0].RelatedEntity;
        ID searchAcctId = a.Id;
        List<Case> cases =
            [SELECT Id, Subject, Description
             FROM Case
             WHERE AccountId = :searchAcctId AND Status != 'Closed'
             WITH USER_MODE];
        string responseData = null;
        if(cases.isEmpty()) {
            responseData = 'There are no open cases.';
        } else {
            for(Case c : cases) {  
                responseData =
                   (responseData != null) ? responseData + '\n' : '';           
                responseData += String.format('Case details: {0}, {1}.',
                    new List<Object>{c.Subject, c.Description});
            }
        }
       
        List<Response> responses = new List<Response>();
        Response res = new Response();
        res.Prompt = responseData;
        responses.add(res);
        return responses;
    }
    
    public class Request {
        @InvocableVariable(required=true)
        public Account RelatedEntity;
    }
    
    public class Response {
        @InvocableVariable
        public String Prompt;
    }
}

Key parts explained:

@InvocableMethod

Required to expose the method to Prompt Builder

label

This is the name you see in the UI (“Open Cases”)

description

Explains what the method does

CapabilityType

Tells Salesforce this method is used for Field Generation Prompt Templates

Method signature rules:

Must be static

Must accept a List of Request

Must return a List of Response

Request Inner Class (Input)

public class Request {

@InvocableVariable(required=true)

public Account RelatedEntity;

}

What this does:

Defines what input the method receives

In this case, it receives an Account record

Important points:

@InvocableVariable exposes the variable to Salesforce

RelatedEntity is automatically populated when the prompt runs

This allows the method to know which Account to process

Response Inner Class (Output)

public class Response {

@InvocableVariable

public String Prompt;

}

What this does:

Defines what data is sent back to Prompt Builder

The Prompt field holds the final text

Einstein uses this text as input context for the LLM

Creating the Field Generation Prompt Template

After the Apex class is ready, follow these steps in Prompt Builder:

Create a custom field on the Account object

Example: Open Case Summary (Text Area – Long)

Open Prompt Builder and create a new prompt template

Prompt Template Type: Field Generation

Object: Account

Object Field: Open Case Summary

Paste a sample “Summarize open cases” prompt into the workspace

Insert resources into the prompt:

Add Account ID as a merge field

Add the Apex resource that fetches open case details

The Apex class provides live case data, and Einstein Generative AI uses it to generate a clear summary.

Final Result

When the prompt runs:

Salesforce calls the Apex class

Open case details are added to the prompt

Einstein Generative AI creates a summary

The summary is saved in the Account field

This gives users up-to-date, AI-generated insights without manual effort.

No comments:

Post a Comment