Loading...
Loading...
Salesforce debugging and troubleshooting skill with log analysis, governor limit detection, and agentic fix suggestions. Parse debug logs, identify performance bottlenecks, analyze stack traces, and automatically suggest fixes.
npx skill4agent add jaganpro/sf-skills sf-debugsf apex list log --target-org [alias]sf apex list log --target-org [alias] --jsonsf apex get log --log-id 07Lxx0000000000 --target-org [alias]sf apex tail log --target-org [alias] --colorsf apex log tail --debug-level FINE --target-org [alias]┌─────────────────────────────────────────────────────────────────┐
│ DEBUG LOG ANALYSIS │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. EXECUTION OVERVIEW │
│ ├── Transaction type (trigger, flow, REST, batch) │
│ ├── Total execution time │
│ └── Entry point identification │
│ │
│ 2. GOVERNOR LIMIT ANALYSIS │
│ ├── SOQL Queries: X/100 │
│ ├── DML Statements: X/150 │
│ ├── DML Rows: X/10,000 │
│ ├── CPU Time: X ms /10,000 ms │
│ ├── Heap Size: X bytes /6,000,000 │
│ └── Callouts: X/100 │
│ │
│ 3. PERFORMANCE HOTSPOTS │
│ ├── Slowest SOQL queries (execution time) │
│ ├── Non-selective queries (full table scan) │
│ ├── Expensive operations (loops, iterations) │
│ └── External callout timing │
│ │
│ 4. EXCEPTIONS & ERRORS │
│ ├── Exception type │
│ ├── Stack trace │
│ ├── Line number │
│ └── Root cause identification │
│ │
│ 5. RECOMMENDATIONS │
│ ├── Immediate fixes │
│ ├── Optimization suggestions │
│ └── Architecture improvements │
│ │
└─────────────────────────────────────────────────────────────────┘| Issue | Detection Pattern | Fix Strategy |
|---|---|---|
| SOQL in Loop | | Query before loop, use Map for lookups |
| DML in Loop | | Collect in List, single DML after loop |
| Non-Selective Query | | Add indexed filter or LIMIT |
| CPU Limit | | Optimize algorithms, use async |
| Heap Limit | | Reduce collection sizes, use FOR loops |
| Callout Limit | | Batch callouts, use Queueable |
Skill(skill="sf-apex", args="Fix [issue type] in [ClassName] at line [lineNumber]")| Category | Points | Key Rules |
|---|---|---|
| Root Cause | 25 | Correctly identify the actual cause, not symptoms |
| Fix Accuracy | 25 | Suggested fix addresses the root cause |
| Performance Impact | 20 | Fix improves performance, doesn't introduce new issues |
| Completeness | 15 | All related issues identified, not just the first one |
| Clarity | 15 | Explanation is clear and actionable |
⭐⭐⭐⭐⭐ 90-100 pts → Expert analysis with optimal fix
⭐⭐⭐⭐ 80-89 pts → Good analysis, effective fix
⭐⭐⭐ 70-79 pts → Acceptable analysis, partial fix
⭐⭐ 60-69 pts → Basic analysis, may miss issues
⭐ <60 pts → Incomplete analysisXX.X (XXXXX)|TIMESTAMP|EVENT_TYPE|[PARAMS]|DETAILS| Event | Meaning | Important For |
|---|---|---|
| Transaction begins | Context identification |
| Method/trigger entry | Call stack tracing |
| Query starts | Query analysis |
| Query ends | Query timing |
| DML starts | DML analysis |
| DML ends | DML timing |
| Exception occurs | Error detection |
| Transaction fails | Critical issues |
| Limit snapshot | Governor limits |
| Heap allocation | Memory issues |
| CPU time used | Performance |
| Callout starts | External calls |
| Level | Shows |
|---|---|
| NONE | Nothing |
| ERROR | Errors only |
| WARN | Warnings and errors |
| INFO | General info (default) |
| DEBUG | Detailed debug info |
| FINE | Very detailed |
| FINER | Method entry/exit |
| FINEST | Everything |
|SOQL_EXECUTE_BEGIN|[line 45]
|SOQL_EXECUTE_END|[1 row]
... (repeats 50+ times)🔴 CRITICAL: SOQL Query in Loop Detected
Location: AccountService.cls, line 45
Impact: 50 queries executed, approaching 100 limit
Pattern: SELECT inside for loop
📝 RECOMMENDED FIX:
Move query BEFORE loop, use Map for lookups:
// Before (problematic)
for (Account acc : accounts) {
Contact c = [SELECT Id FROM Contact WHERE AccountId = :acc.Id LIMIT 1];
}
// After (bulkified)
Map<Id, Contact> contactsByAccount = new Map<Id, Contact>();
for (Contact c : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accountIds]) {
contactsByAccount.put(c.AccountId, c);
}
for (Account acc : accounts) {
Contact c = contactsByAccount.get(acc.Id);
}|SOQL_EXECUTE_BEGIN|[line 23]|SELECT Id FROM Lead WHERE Status = 'Open'
|SOQL_EXECUTE_END|[250000 rows queried]🟠 WARNING: Non-Selective Query Detected
Location: LeadService.cls, line 23
Rows Scanned: 250,000
Filter Field: Status (not indexed)
📝 RECOMMENDED FIX:
Option 1: Add indexed field to WHERE clause
Option 2: Create custom index on Status field
Option 3: Add LIMIT clause if not all records needed
// Before
List<Lead> leads = [SELECT Id FROM Lead WHERE Status = 'Open'];
// After (with additional selective filter)
List<Lead> leads = [SELECT Id FROM Lead
WHERE Status = 'Open'
AND CreatedDate = LAST_N_DAYS:30
LIMIT 10000];|LIMIT_USAGE_FOR_NS|CPU_TIME|9500|10000🔴 CRITICAL: CPU Time Limit Approaching (95%)
Used: 9,500 ms
Limit: 10,000 ms (sync) / 60,000 ms (async)
📝 ANALYSIS:
Top CPU consumers:
1. StringUtils.formatAll() - 3,200 ms (line 89)
2. CalculationService.compute() - 2,800 ms (line 156)
3. ValidationHelper.validateAll() - 1,500 ms (line 45)
📝 RECOMMENDED FIX:
1. Move heavy computation to @future or Queueable
2. Optimize algorithms (O(n²) → O(n))
3. Cache repeated calculations
4. Use formula fields instead of Apex where possible|HEAP_ALLOCATE|[5800000]
|LIMIT_USAGE_FOR_NS|HEAP_SIZE|5800000|6000000🔴 CRITICAL: Heap Size Limit Approaching (97%)
Used: 5.8 MB
Limit: 6 MB (sync) / 12 MB (async)
📝 ANALYSIS:
Large allocations detected:
1. Line 34: List<Account> - 2.1 MB (50,000 records)
2. Line 78: Map<Id, String> - 1.8 MB
3. Line 112: String concatenation - 1.2 MB
📝 RECOMMENDED FIX:
1. Use SOQL FOR loops instead of querying all at once
2. Process in batches of 200 records
3. Use transient keyword for variables not needed in view state
4. Clear collections when no longer needed
// Before
List<Account> allAccounts = [SELECT Id, Name FROM Account];
// After (SOQL FOR loop - doesn't load all into heap)
for (Account acc : [SELECT Id, Name FROM Account]) {
// Process one at a time
}|EXCEPTION_THROWN|[line 67]|System.NullPointerException: Attempt to de-reference a null object
|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object🔴 EXCEPTION: System.NullPointerException
Location: ContactService.cls, line 67
Message: Attempt to de-reference a null object
📝 STACK TRACE ANALYSIS:
ContactService.getContactDetails() - line 67
└── AccountController.loadData() - line 34
└── trigger AccountTrigger - line 5
📝 ROOT CAUSE:
Variable 'contact' is null when accessing 'contact.Email'
Likely scenario: Query returned no results
📝 RECOMMENDED FIX:
// Before
Contact contact = [SELECT Email FROM Contact WHERE AccountId = :accId LIMIT 1];
String email = contact.Email; // FAILS if no contact found
// After (null-safe)
List<Contact> contacts = [SELECT Email FROM Contact WHERE AccountId = :accId LIMIT 1];
String email = contacts.isEmpty() ? null : contacts[0].Email;
// Or using safe navigation (API 62.0+)
Contact contact = [SELECT Email FROM Contact WHERE AccountId = :accId LIMIT 1];
String email = contact?.Email;| Command | Purpose |
|---|---|
| List available logs |
| Download specific log |
| Stream logs real-time |
| Delete logs |
# Create trace flag for user
sf data create record \
--sobject TraceFlag \
--values "TracedEntityId='005xx000000000' LogType='USER_DEBUG' DebugLevelId='7dlxx000000000' StartDate='2024-01-01T00:00:00' ExpirationDate='2024-01-02T00:00:00'" \
--target-org my-sandbox
# Set default debug level
sf config set org-api-version=62.0# Use Developer Console or Tooling API
sf data query \
--query "SELECT Id FROM Account WHERE Name = 'Test'" \
--target-org my-sandbox \
--use-tooling-api \
--plan┌─────────────────────────────────────────────────────────────────┐
│ AGENTIC DEBUG LOOP │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 1. Fetch debug logs from the failing operation │
│ 2. Parse logs and identify all issues │
│ 3. Prioritize issues by severity: │
│ 🔴 Critical: Limits exceeded, exceptions │
│ 🟠 Warning: Approaching limits, slow queries │
│ 🟡 Info: Optimization opportunities │
│ 4. For each critical issue: │
│ a. Read the source file at identified line │
│ b. Generate fix using sf-apex skill │
│ c. Deploy fix using sf-deploy skill │
│ d. Re-run operation and check new logs │
│ 5. Report final status and remaining warnings │
│ │
└─────────────────────────────────────────────────────────────────┘| Skill | When to Use | Example |
|---|---|---|
| sf-apex | Generate fixes for identified issues | |
| sf-testing | Run tests to reproduce issues | |
| sf-deploy | Deploy fixes | |
| sf-data | Create test data for debugging | |
| Resource | Warning Threshold | Critical Threshold |
|---|---|---|
| SOQL Queries | 80/100 (80%) | 95/100 (95%) |
| DML Statements | 120/150 (80%) | 145/150 (97%) |
| CPU Time | 8,000/10,000 ms | 9,500/10,000 ms |
| Heap Size | 4.8/6 MB | 5.7/6 MB |
| Callouts | 80/100 | 95/100 |
| Category | Acceptable | Needs Optimization |
|---|---|---|
| Query Time | < 100ms | > 500ms |
| Rows Scanned | < 10,000 | > 100,000 |
| Selectivity | Indexed filter | Full table scan |
| Document | Description |
|---|---|
| debug-log-reference.md | Complete debug log event reference |
| cli-commands.md | SF CLI debugging commands |
| benchmarking-guide.md | Dan Appleman's technique, real-world benchmarks |
| log-analysis-tools.md | Apex Log Analyzer, manual analysis patterns |
| Template | Description |
|---|---|
| cpu-heap-optimization.cls | CPU and heap optimization patterns |
| benchmarking-template.cls | Ready-to-run benchmark comparisons |
| soql-in-loop-fix.cls | SOQL bulkification pattern |
| dml-in-loop-fix.cls | DML bulkification pattern |
| null-pointer-fix.cls | Null-safe patterns |
sf/plugin install github:Jaganpro/sf-skills/sf-debug