I’m here to talk about a quiet, yet growing challenge in every mature Salesforce org — formula fields.
Over time, these fields become deeply nested, reference multiple objects, and use expensive functions like ISCHANGED, VLOOKUP, and PRIORVALUE. While powerful, they often hide in plain sight — until they cause Apex CPU timeouts, flow failures, or performance lags across the platform.
As orgs scale, it becomes harder to track which formulas introduce risk — especially when logic is split across flows, triggers, and declarative tools.
That’s where This Salesforce Lightning Web Component (LWC) helps developers and admins proactively detect complex formula fields that may risk hitting Apex CPU time limits. It analyzes all formula fields on a specific object and calculates their:
🔍 Metric | 🧠 Meaning |
---|---|
🧬 Nesting depth | How deep the formula nesting is (e.g., nested IF , AND , OR logic). Deep nesting can hit CPU limits. |
🔗 Cross-object hops | How many relationships the formula travels across (Account.Owner.Profile.Name = 2 hops). More hops = more CPU. |
🧠 Heavy function count | Count of expensive functions like ISCHANGED , PRIORVALUE , VLOOKUP , GEOLOCATION , DISTANCE . These are CPU-heavy. |
🔥 CPU risk level | Based on depth, hops, heavy functions, length, and unbalanced parentheses. (High / Medium / Low). |
💣 Formula too long | Boolean flag if formula exceeds 3000 characters. Can impact performance. |
🧍 Uses $User / $RecordType | Checks if formula references current user or record type — often used in dynamic formulas. |
📈 CPU Score | A numerical score derived from multiple factors to quantify complexity/risk. |
🚩 Red Flags | Text field highlighting specific issues found, e.g., “Excessive nesting”, “Unbalanced parentheses”, etc. |
It uses the Tooling API securely via a Named Credential, removing the need for manual token management.Plus it is integrated with Gemini AI and so you will get the Optimised version for your formulas.
Salesforce formulas can silently contribute to performance bottlenecks, especially when:
🧬 Nesting gets deep Deeply nested IF, AND, or CASE statements increase evaluation complexity.
🔗 Cross-object references are chained Referencing multiple parent objects (e.g., Opportunity.Account.Owner.Profile.Name) increases data traversal and CPU usage.
🔁 Used in multiple automations When the same formula field is used in multiple Flows, validation rules, or process builders, it's recalculated multiple times.
🧠 Contain heavy functions Expensive functions like VLOOKUP, ISCHANGED, PRIORVALUE, DISTANCE, GEOLOCATION can cause spikes in CPU time.
🕵️ Used in record-triggered Flows or Apex When formulas are accessed via triggers or flows, Salesforce recalculates them, impacting total transaction time.
🧮 Used in List Views or Reports Formula fields on frequently-used list views or report filters slow down loading and query times.
🧱 Include hardcoded logic or IDs Makes formulas brittle and harder to maintain, often leading to downstream logic errors or misfires.
🔁 Have repeated expressions Repeating the same logic multiple times within a formula increases processing cost unnecessarily.
📅 Use date/time logic excessively Functions like NOW(), TODAY(), DATEVALUE() can recalculate on every view/load, increasing overhead.
🧾 Evaluated on large data volumes In batch Apex or mass updates (e.g., data loads), formula fields contribute to CPU usage per record.
In short, complex formulas don’t just live in one place — they echo across your org, and without visibility, they quietly accumulate as technical debt this tool helps surface these risks — and now, thanks to AI, even helps you solve them.
- Secure Tooling API access via Named Credential
- Lightweight LWC with dynamic badge indicators
- Easily extensible

To use the Gemini API, you'll need to obtain an API key. Follow these steps to get your API key from Google.
🔹 Step 1: Go to Google AI Studio Google AI Studio Log in with your Google/Gmail account 🔹 Step 2: Get Your API Key Click your Google account icon (top-right corner) Choose “Get API Key” Click “Create API Key” Copy this key — you’ll use it as the x-goog-api-key header for calling Gemini APIs.
You can use my API key since it's free, but I recommend generating your own in case it becomes paid later—you’ll then have full control.
- Keep Your API Key Confidential: Do not share your API key publicly or include it in client-side code.
- Regenerate Keys if Compromised: If you believe your API key has been compromised, regenerate it immediately from the Gemini dashboard.
- Monitor Usage: Regularly check your Gemini usage to ensure there are no unexpected activities.
By following these steps, you'll be able to obtain and securely use your Gemini API key for accessing the Gemini services.



To configure Salesforce to communicate with the Gemini API, follow these steps to add a remote site setting.
-
Log in to Salesforce:
- Go to your Salesforce instance and log in with an account that has administrative privileges.
-
Navigate to Remote Site Settings:
- Click on the Setup icon (the gear icon) in the upper right corner.
- In the quick find box, type
Remote Site Settings
and select it from the dropdown list.
-
Create a New Remote Site:
- Click the New Remote Site button.
-
Fill in the Remote Site Details:
- Remote Site Name: Enter a name for the remote site. Example:
Gemini_2_5_Flash
. - Remote Site URL: Enter
https://generativelanguage.googleapis.com/
.- Ensure you use
https://
for the secure protocol.
- Ensure you use
- Description: Optionally, add a description such as
Remote site for Gemini API
.
- Remote Site Name: Enter a name for the remote site. Example:
-
Save the Remote Site:
- Click the Save button to create the remote site setting.
-
Verify the Remote Site Setting:
- After saving, you should see the new remote site listed in the Remote Site Settings. You can click on the name to view or edit details if needed.
- Security and Access: Ensure that the remote site setting matches the Gemini API URL exactly. The protocol (
https://
) and domain must be correct. - Testing Connectivity: Use an HTTP request tool or write Apex code to test if Salesforce can access the Gemini API.
- Apex Integration: Consider using named credentials for storing authentication securely and simplifying API calls.
By following these steps, you can configure Salesforce to communicate with the Gemini API. Ensure that the URL and other details are accurate to avoid any issues.
-
Go to: Setup → App Manager → New Connected App
-
Fill in:
- Name:
ToolingAPI_ConnectedApp
- API Name: (auto-filled)
- Contact Email: (your email)
- Name:
-
✅ Enable OAuth Settings:
-
☑️ Enable OAuth Settings
-
Callback URL:
https://login.salesforce.com/services/oauth2/callback
-
Selected OAuth Scopes:
Access and manage your data (api)
Perform requests on your behalf at any time (refresh_token, offline_access)
Full access (full)
-
-
Click Save. 🔑 Note the Consumer Key and Consumer Secret
⏳ Wait 2–10 minutes for it to become available.
-
Go to: Setup → Auth. Providers → New
-
Provider Type:
Salesforce
-
Fill:
- Name:
Salesforce_Tooling_Auth
- Consumer Key: from Connected App
- Consumer Secret: from Connected App
- Authorize Endpoint URL:
https://login.salesforce.com/services/oauth2/authorize
- Token Endpoint URL:
https://login.salesforce.com/services/oauth2/token
- Name:
-
Click Save 🔁 Note the Callback URL Salesforce generates (you’ll use it in the next step)
-
Go back to: Setup → App Manager → Find your Connected App → Edit
-
Replace the old Callback URL with the one from the Auth Provider (
https://...force.com/.../services/authcallback/...
) -
Save again.
-
Go to: Setup → Named Credentials → New
-
Fill:
Field | Value |
---|---|
Label | ToolingAPI |
Name | ToolingAPI |
URL | https://yourInstance.salesforce.com (e.g., https://na123.salesforce.com ) |
Identity Type | Named Principal |
Authentication Protocol | OAuth 2.0 |
Authentication Provider | Salesforce_Tooling_Auth |
Scope | full refresh_token offline_access |
Generate Authorization Header | ☑️ Yes |
Allow Callouts | ☑️ Yes |
- Click Save → then click Authenticate to complete the OAuth flow.
-
Navigate to App(FormulaSniffR) Open the App Page (FormulaSniffR).
-
On load, the component:
- Reads the
objectApiName
from the user. - Uses Tooling API to retrieve all formula fields for the selected object.
- Displays risk indicators for each field
- Formula updates from the UI are supported only in sandbox environments, not in production.
- Reads the
You can see the forecast by clicking on the chart icon,plus you can send email to all admins regarding the complexity
Attaching a sample email in this branch. FormulaSniffR Forecast & Summary - Inefficient_Formula__c (PDF)
You can replace the formula with the optimised one by clicking on the paste icon.
- API Version:
60.0
(Summer '24) - Works with: Salesforce DX, Scratch Orgs, Sandboxes, Dev Orgs
- No secrets (tokens or keys) are stored in this repo.