The Dynamic Apex Blueprint: Building 'Shape-Shifting' Actions for Agentforce 🤖📜
Codeforce Chronicles #107: The Generics Revolution
By Shruthi MN | Salesforce Coach & Agentforce Champion
In the old world of Salesforce, we wrote one Apex action for one specific task. Need to update a Lead? Write an Invocable. Need to update a Case? Write another.
But Agentforce doesn’t want 500 static buttons. It wants a set of highly capable, "generic" tools that it can configure on the fly. Today, I’m sharing my blueprint for Dynamic Apex for AI—the secret to writing reusable code that makes your agents 10x smarter.
The Concept: Move from "Static" to "Generic" 🧠
As a Salesforce Coach, I use this analogy: Standard Apex is like a screwdriver that only fits one screw. Dynamic Apex is like a high-end power tool with interchangeable heads.
By using SObject generic types and Type.forName(), we can create Invocable Methods that accept the Object Name, the Field Map, and the Operation as input parameters. The Agent decides what it needs at runtime.
My 3-Step "Dynamic" Framework 🛠️
1. The Generic Input Wrapper
Instead of specific variables, I use a List<Request> class that accepts a String for the Object API name. This allows one single Apex class to handle Leads, Accounts, or even Custom Objects.
2. Schema Describes for Safety
I never "hard-code." I use Schema.getGlobalDescribe() to validate that the Agent is asking for a real object and that it has the correct permissions. This is #CleanCore at its finest.
3. The "Smart Routing" Logic
Using a single Invocable, my Agent can say: "I need to update the 'Rating' to 'Hot' on this record, but only if the 'Status' is 'New'." The Apex handles the DML dynamically, returning a success or failure message that the Agent can actually understand.
3 "War Story" Use Cases for Dynamic Actions 🏗️
1. The Universal Field Updater: I built one action that allowed an Agent to update any field on any object. We replaced 15 separate Flow actions with one dynamic Apex class, reducing technical debt by 90%.
2. The Autonomous Auditor: An agent that scans records and fixes data discrepancies on the fly. By passing the field names dynamically, the Agent can "learn" which fields are messy without me redeploying code.
3. Agentic Cross-Object Routing: A Lead comes in, and the Agent needs to check a related custom "Territory" object. My dynamic action allows the Agent to query that related data without needing a pre-built relationship in a Flow.
Coach Shruthi’s Knowledge Check 🧠✍️
If you're aiming for that Lead Architect role, answer these in the comments:
Q1: Which Apex method is used to turn a String (like "Account") into a dynamic SObject type?
A) Object.cast()
B) Schema.getGlobalDescribe().get(objectName).newSObject()
C) Database.query()
Q2: Why is "Dynamic Apex" safer for Agentforce grounding than hard-coded logic?
Level Up with Codeforce Academy 🎓🏢
Don't just learn to code—learn to architect the future.
Self-Paced Mastery: ₹3,000 (Dynamic Apex & AI modules included).
Live Coaching Intensive: ₹5,000 (We build a Dynamic Agentic Mesh together).
Note: I believe in the power of your portfolio. I provide no placement guarantees—I provide the skills that make guarantees unnecessary.
Book Your Free Demo: 📲 Text: 6361348307 | 📧 Email: shrutinarasi@gmail.com
Support the Chronicles! 🎥🎬
Help me reach 10,000 views and 100 YouTube subscribers!
Subscribe to my YouTube for the "Dynamic Apex" live build:
https://www.youtube.com/@CodeForceChronicles 🔔Follow the blog: [https://salesforcecodeforcechronicles.blogspot.com/2026/04/ssr-is-not-enough-why-your-high-traffic.html]
Share this post! If you comment "DYNAMIC" below, I will send you my "Agentforce Strategic Discovery & Reasoning Boundaries" PDF! 🎁🛡️
