Skip to content
Back to skills

041 Anonymous Apex Guide 63726248

ASecurity

Using anonymous Apex for complex data operations.

  • 11 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added October 11, 2026
code-qualitybashtestingdebugging

Works with

  • cli

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 041-anonymous-apex-guide_63726248 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 041 Anonymous Apex Guide 63726248?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 041 Anonymous Apex Guide 63726248
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-041-anonymous-apex-guide-63726248/badge)](https://www.skillsdirectory.com/skills/tools-only-041-anonymous-apex-guide-63726248)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Anonymous Apex Guide

Using anonymous Apex for complex data operations.

## When to Use Anonymous Apex

- Complex data setup requiring Apex logic
- Testing triggers with specific data patterns
- One-time data migrations
- Debugging and troubleshooting

## sf CLI Execution

### From File
```bash
sf apex run --file setup-data.apex --target-org myorg
```

### Interactive
```bash
sf apex run --target-org myorg
# Then type Apex code and press Ctrl+D
```

## Common Patterns

### Bulk Data Creation
```apex
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 500; i++) {
    accounts.add(new Account(
        Name = 'Test Account ' + i,
        Industry = 'Technology'
    ));
}
insert accounts;
System.debug('Created ' + accounts.size() + ' accounts');
```

### Data Transformation
```apex
List<Account> accounts = [
    SELECT Id, Name, Industry
    FROM Account
    WHERE Name LIKE 'Old%'
];

for (Account acc : accounts) {
    acc.Name = acc.Name.replace('Old', 'New');
}

update accounts;
```

### Testing Trigger Logic
```apex
// Setup test data
Account acc = new Account(Name = 'Trigger Test');
insert acc;

// Force trigger to fire
acc.Industry = 'Technology';
update acc;

// Verify results
acc = [SELECT Id, Field__c FROM Account WHERE Id = :acc.Id];
System.debug('Result: ' + acc.Field__c);
```

## Error Handling

```apex
try {
    insert accounts;
} catch (DmlException e) {
    System.debug('Error: ' + e.getMessage());
    for (Integer i = 0; i < e.getNumDml(); i++) {
        System.debug('Row ' + e.getDmlIndex(i) + ': ' + e.getDmlMessage(i));
    }
}
```

## Limits in Anonymous Apex

| Limit | Value |
|-------|-------|
| SOQL Queries | 100 |
| DML Rows | 10,000 |
| CPU Time | 10,000 ms |
| Heap Size | 6 MB |

## Best Practices

1. **Test in sandbox first** - Validate before production
2. **Add debug statements** - Track progress
3. **Handle errors gracefully** - Use try/catch
4. **Keep scripts idempotent** - Safe to re-run

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in code-quality →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.