Loading...

Sales representatives often work with Salesforce records that contain a lot of information, including emails, tasks, events, and Chatter activity. Going through all this information manually can make it easy to miss important details.

With Salesforce LWC and Models API, you can build an AI-powered record assistant that brings this information together, summarizes important details, and provides useful suggestions directly inside Salesforce. 

In this guide, we’ll build a record assistant that retrieves Salesforce record data, processes it through the Models API, and displays the generated response in a side-panel interface.

Step 1

Prerequisites

Before getting started, you should have a basic understanding of:

  • Salesforce metadata and Custom Metadata Types
  • Lightning Web Components (LWC)
  • Apex
  • Salesforce authentication and integration concepts

How the Salesforce Record Assistant Works

The solution follows a simple flow where Salesforce record information is collected, securely passed through the API layer, and then displayed as an AI-generated response inside the LWC.

Salesforce Record → Apex Controller → Metadata-driven API Layer → Named Credential → Models API → AI-generated Summary → LWC Side Panel UI

Why this architecture?

  • Metadata-driven configuration: API endpoints, headers, and settings can be managed through metadata instead of being hardcoded into Apex.
  • USER_MODE: Salesforce applies CRUD and field-level security while querying records, helping keep the implementation aligned with platform security.
  • Named Credentials: Authentication, endpoints, and secrets are managed centrally instead of being hardcoded in Apex.
  • Dynamic prompts: The assistant can use the current record, related activities, and user messages to generate more relevant responses.
  • Contextual routing: The component can identify the current Salesforce page or record and adjust its response based on that context.

Step-by-Step Guide

Follow the steps below to create the metadata, authentication setup, Apex controller, LWC, and finally add the assistant to your Salesforce application.

Step 1: Create the Custom Metadata Type

  • Go to Setup.
  • Search for Custom Metadata Types in Quick Find.
  • Click New Custom Metadata Type.
  • For this example, enter API Handler as the Label.
  • Click Save.

Step 1.1

Step 1.2

Step 2: Create the Metadata Fields

  • Open the API Handler Custom Metadata Type.
  • Create the required fields based on your implementation.

These fields will store the API configuration details that are later used by the Apex controller.,

Step 2

 

Step 3: Create the Models API Metadata Record

  • Click Manage API Handlers.
  • Click New.
  • Configure the metadata record with the following values:
  • Click Save

Step 3

Property Value
Label ModelsAPI
Batch Delay 12,000
Named Credential Name Salesforce_ModelsAPI_Named_Credential
Is Active Checked
Endpoint /einstein/platform/v1/models/{modelName}/chat-generations
Header Parameter {
“x-sfdc-app-context”: “EinsteinGPT”,
“x-client-feature-id”: “ai-platform-models-connected-app”,
“Content-Type”: “application/json;charset=utf-8”
}
Method POST
Retry Delay 12,000
Max Retry Limit 3
Timeout 120,000


This metadata record keeps the API configuration separate from the Apex code, making it easier to maintain.

Step 3.2

Step 4: Create an External Client App

  • Go to Setup → App Manager.
  • Click New External Client App.

Step 4

  • Enter the required application details

Setting Value
External Client App Name ModelsAPI OAuth
API Name ModelsAPI_OAuth
Contact Email yourEmail
Distribution State Local
Enable OAuth Checked
Callback URL https://login.salesforce.com
OAuth Scopes Manage user data via APIs (api)
Perform requests at any time (refresh_token, offline_access)
Access chatbot services (chatbot_api)
Access the Salesforce API Platform (sfap_api)
Enable Client Credentials Flow Checked
Require secret for Web Server Flow Checked
Require Proof Key for Code Exchange (PKCE) extension for Supported Authorization Flows Unchecked
Require secret for Refresh Token Flow Checked
Issue JSON Web Token (JWT)-based access tokens for named users Checked

 

  • Click Save

Step 5: Configure OAuth Settings

  • Open Policies → Edit.

Step 5

  • Enable Client Credentials Flow and Run As (Username)

Step 5.1

  • Click Save

Step 6: Retrieve the Consumer Key and Secret

  • Open the Settings tab of the External Client App.
  • Click Consumer Key and Secret.

Step 6

  • Enter the code you will receive in the email.

Step 6.1

  • Copy the Consumer Key and Consumer Secret

Step 7: Create the External Credential

  • Go to Setup → Named Credentials.
  • Open External Credentials.
  • Click New.

Step 7

  • Enter the required details for the external credential.
  • Click Save.

Step 7.1

The External Credential stores the authentication information needed to securely connect with the Models API. 

Step 8: Configure the External Credential Principal

  • Open the External Credential you created.
  • Go to Principals.
  • Click New.

Step 8

  • Configure the principal using the required authentication details.
  • Click Save.

Step 8.1

This principal connects the external credential with the authentication setup used for the API connection.

Step 9: Create the Named Credential

  • Go to Named Credentials.
  • Click New.

Step 9

  • Enter the required details for the Models API connection.

Step 9.1

  • Select the external credential configured in the previous steps.
  • Click Save.

The Named Credential keeps the endpoint and authentication details in one place, so they do not need to be hardcoded in Apex.

Step 10: Add the AI Assistant Static Resource

  • Go to Setup → Static Resources.
  • Click New.
  • Enter a suitable name for the static resource.
  • Upload the background image you want to use for the assistant.

Step 10

  • Click Save.

Step 10.1

Step 11: Create the Apex Controller

The Apex controller retrieves the Salesforce record and its related activities, prepares the information, and sends it to the Models API.

  • Open your Salesforce DX project.
  • Run the Apex class creation command.
    sfdx force:apex:class:create -n sidePanelAIAssistantHandler
  • Add the following Apex code.

Step 12: Create the Lightning Web Component

  • Run the LWC creation command.
    sfdx force:lightning:component:create –type lwc -n SidePanelAIAssistant -d force-app/main/default/lwc
  • Add the component files.

SidePanelAIAssistant

HTML

JS

CSS

XML

Step 13: Deploy the AI Assistant Components

  • Right-click on the Apex file → SFDX: Deploy This Source to Org
  • Right-click on the XML file → SFDX: Deploy This Source to Org

Step 14: Add the AI Assistant to the Utility Bar

  • Go to Setup → App Manager.
  • Find the Salesforce app where you want to add the assistant.
  • Click Edit.
  • Open Utility Items (Desktop Only).

Step 14

Step 14.1

  • Open Utility Items (Desktop Only).

Step 14.2

  • Click Add Utility Item.
  • Select the AI Assistant component.

Step 14.3

  • Configure its label, icon, width, height, and startup behavior.

Step 14.4

  • Save the app.

 

Current Structure Limitations

Before using this approach in production, there are a few limitations worth considering.

Large Payload Handling

The solution retrieves the main record, Tasks, Events, and Chatter activity together. Records with a large amount of activity can therefore create a large payload.

Prompt Injection Concerns

User-generated content from Chatter posts, comments, or other sources may contain misleading instructions. Additional validation and filtering may be needed before sending this information to the model.

Token Optimization

Sending unnecessary information to the model can increase token usage and processing time. Filtering the most relevant data can help keep prompts smaller and more efficient.

Long Chatter Feed Summarization

Records with extensive Chatter history can create large contextual inputs. Filtering or prioritising recent activity can help improve response time.

API Consumption

Frequent model requests can increase API usage and related costs. Caching, throttling, and request optimization should be considered as the solution grows.

Conclusion

Building a record assistant with Salesforce LWC and Models API brings AI capabilities directly into Salesforce, helping sales representatives understand record information without manually checking every activity and related detail. 

By combining Apex, metadata, Named Credentials, and LWC, teams can create a Salesforce AI assistant that works with records, Tasks, Events, and Chatter while keeping the experience inside Salesforce.

For developers exploring Models API Salesforce implementations, this approach offers a practical foundation. Before production, optimize payloads, protect prompts, manage API usage, and carefully handle sensitive information.

Watch Demo Video

No Data Found.

Related Articles
How to Build an AI Email Generator for Salesforce Cases Using Salesforce Models API

Support agents spend a lot of time writing the same kind of customer emails, whether it is a case update, resolution message, or simple follow-up after an issue. An email generator for Salesforce can help reduce this repetitive work by creating a first draft directly from the Case record, using the information already available in […]

Read More
How to Integrate Purchase Orders Between NikoHealth and Salesforce

Purchase Orders are used to manage inventory procurement, vendor orders, product replenishment, and supply chain operations. Using MultiSync Made Easy, organizations can synchronize Purchase Orders between NikoHealth and Salesforce while maintaining relationships with: Vendors Products Purchase Order Items Locations In this guide, we will configure: NikoHealth → Salesforce Purchase Order Synchronization Salesforce → NikoHealth Purchase […]

Read More
How to Integrate Invoices Between NikoHealth and Salesforce

Invoices are a critical part of the healthcare billing process. They contain billing information, insurance details, diagnosis codes, service lines, HCPCS codes, charges, balances, and payment-related information. Using NikoHealth with Salesforce, organizations can automatically synchronize Invoice records between both platforms while maintaining relationships with: Orders Patients With Salesforce NikoHealth integration through MultiSync Made Easy, organizations […]

Read More
How to Integrate Order Records Between NikoHealth and Salesforce

In this guide, we’ll walk through how to synchronize Order records between NikoHealth and Salesforce using MultiSync Made Easy. Before proceeding, ensure that: NikoHealth is already authorized with Salesforce. Patient synchronization is already configured. The Order object exists in Salesforce. This Salesforce NikoHealth integration guide covers: NikoHealth → Salesforce Order Synchronization Salesforce → NikoHealth Order […]

Read More
How to Integrate Documents Between NikoHealth and Salesforce

Healthcare organizations manage a wide range of patient documents, from prescriptions and medical orders to other important records. Keeping these files updated across NikoHealth with Salesforce can be difficult when documents are uploaded and managed manually.  A reliable Salesforce document management process helps ensure patient files stay organized and accessible in one place. In this […]

Read More
How to Integrate Medical Records Between NikoHealth and Salesforce

Medical Records contain important patient diagnosis information used by healthcare providers for treatment, billing, authorizations, prescriptions, and order processing. With MultiSync Made Easy, organizations can simplify NikoHealth and Salesforce synchronization by automatically syncing Medical Records between both platforms while maintaining accurate Patient relationships. In this guide, we will configure: NikoHealth → Salesforce Medical Record Synchronization […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203669505
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
CA Flag Canada
HIC Global Solutions INC
6D - 7398 Yonge St #1124 Thornhill, ON L4J 8J2 Canada +1(262) 310-7818