Skip to content

forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-data-manage

platform-data-manage

Verified

Salesforce data operations with 130-point scoring. Use to create, update, delete, bulk import/export, generate test data, and clean up org records via sf CLI and anonymous Apex. TRIGGER on creating test data, bulk import/export, sf data CLI commands, data-factory patterns for Apex tests, or seeding/cleaning org records. DO NOT TRIGGER for SOQL query writing only (use platform-soql-query), Apex test execution (use platform-apex-test-run), or metadata deployment (use platform-metadata-deploy).

FileKindSize
README.mdmarkdown4.5 KB
SKILL.mdskill md10.5 KB
assets/bulk/bulk-insert-10000.apexother12.3 KB
assets/bulk/bulk-insert-200.apexother12.4 KB
assets/bulk/bulk-insert-500.apexother13.1 KB
assets/bulk/bulk-upsert-external-id.apexother13.4 KB
assets/cleanup/delete-by-created-date.apexother15.9 KB
assets/cleanup/delete-by-name.apexother12.9 KB
assets/cleanup/delete-test-data.apexother16.6 KB
assets/cleanup/rollback-transaction.apexother12.4 KB
assets/csv/account-import.csvother1.8 KB
assets/csv/contact-import.csvother1.9 KB
assets/csv/custom-object-import.csvother1.1 KB
assets/csv/opportunity-import.csvother1.6 KB
assets/factories/account-factory.apexother5.6 KB
assets/factories/case-factory.apexother8.1 KB
assets/factories/contact-factory.apexother5.9 KB
assets/factories/custom-object-factory.apexother10.8 KB
assets/factories/event-factory.apexother9.6 KB
assets/factories/hierarchy-factory.apexother16.0 KB
assets/factories/lead-factory.apexother6.4 KB
assets/factories/opportunity-factory.apexother7.3 KB
assets/factories/task-factory.apexother8.1 KB
assets/factories/user-factory.apexother8.8 KB
assets/json/account-contact-tree.jsonconfig3.6 KB
assets/json/account-opportunity-tree.jsonconfig3.3 KB
assets/json/full-hierarchy-tree.jsonconfig5.8 KB
assets/soql/aggregate.soqlother12.3 KB
assets/soql/child-to-parent.soqlother9.8 KB
assets/soql/parent-to-child.soqlother8.6 KB
assets/soql/polymorphic.soqlother11.1 KB
assets/soql/subquery.soqlother16.2 KB
references/anonymous-apex-guide.mdmarkdown2.0 KB
references/bulk-operations-guide.mdmarkdown1.8 KB
references/bulk-testing-example.mdmarkdown4.9 KB
references/cleanup-rollback-example.mdmarkdown8.2 KB
references/cleanup-rollback-guide.mdmarkdown1.7 KB
references/crud-workflow-example.mdmarkdown4.0 KB
references/governor-limits-reference.mdmarkdown1.8 KB
references/orchestration.mdmarkdown7.0 KB
references/relationship-query-examples.mdmarkdown5.7 KB
references/sf-cli-data-commands.mdmarkdown2.8 KB
references/soql-relationship-guide.mdmarkdown2.0 KB
references/test-data-best-practices.mdmarkdown3.2 KB
references/test-data-factory-usage.mdmarkdown7.6 KB
references/test-data-patterns.mdmarkdown2.4 KB
scripts/soql_validator.pyscript10.9 KB
scripts/validate_data_operation.pyscript13.5 KB

references/bulk-testing-example.md

View on GitHub
<!-- Parent: platform-data-manage/SKILL.md -->
# Bulk Testing Example

Testing Apex triggers and flows with bulk data operations.

## Scenario

Test an Account trigger that:
- Fires on insert with 200+ records
- Updates a custom field based on Industry
- Creates a related Task for high-value accounts

## Why 201 Records?

Salesforce processes triggers in batches of 200. Testing with 201+ records ensures:
- Trigger handles batch boundaries
- No governor limit violations in loops
- SOQL/DML operations are bulkified

## Method 1: Anonymous Apex Factory

### Create Test Data
```apex
// Create 251 Accounts to test trigger bulkification
List<Account> accounts = new List<Account>();

List<String> industries = new List<String>{
    'Technology', 'Healthcare', 'Finance', 'Manufacturing', 'Retail'
};

for (Integer i = 0; i < 251; i++) {
    accounts.add(new Account(
        Name = 'BulkTest Account ' + i,
        Industry = industries[Math.mod(i, industries.size())],
        AnnualRevenue = Math.round(Math.random() * 10000000)
    ));
}

// Single DML - triggers fire in batches of 200
insert accounts;

System.debug('Created ' + accounts.size() + ' accounts');
System.debug('SOQL Queries Used: ' + Limits.getQueries() + '/' + Limits.getLimitQueries());
System.debug('DML Statements: ' + Limits.getDmlStatements() + '/' + Limits.getLimitDmlStatements());
```

Save as `bulk-test-accounts.apex` and run:
```bash
sf apex run --file bulk-test-accounts.apex --target-org dev
```

## Method 2: CSV Bulk Import

### Create CSV File
```csv
Name,Industry,AnnualRevenue
BulkTest Account 1,Technology,1000000
BulkTest Account 2,Healthcare,2000000
BulkTest Account 3,Finance,5000000
... (251 rows)
```

### Import via sf CLI
```bash
sf data import bulk \
  --file accounts-bulk.csv \
  --sobject Account \
  --target-org dev \
  --wait 30
```

## Method 3: JSON Tree Import

For hierarchical test data with relationships:

```json
{
  "records": [
    {
      "attributes": {"type": "Account", "referenceId": "AccountRef1"},
      "Name": "BulkTest Parent 1",
      "Industry": "Technology",
      "Contacts": {
        "records": [
          {
            "attributes": {"type": "Contact"},
            "FirstName": "Test",
            "LastName": "Contact 1"
          }
        ]
      }
    }
  ]
}
```

```bash
sf data import tree \
  --files bulk-hierarchy.json \
  --target-org dev
```

## Verify Trigger Executed Correctly

### Check Trigger Results
```bash
sf data query \
  --query "SELECT Id, Name, Industry, Custom_Field__c FROM Account WHERE Name LIKE 'BulkTest%' LIMIT 10" \
  --target-org dev \
  --json
```

### Check Related Tasks Created
```bash
sf data query \
  --query "SELECT Id, Subject, WhatId, What.Name FROM Task WHERE What.Name LIKE 'BulkTest%'" \
  --target-org dev
```

### Count Records
```bash
sf data query \
  --query "SELECT COUNT(Id) total FROM Account WHERE Name LIKE 'BulkTest%'" \
  --target-org dev
```

## Test Bulk Update

```apex
// Update all test records - triggers fire again
List<Account> accounts = [
    SELECT Id, Name, Industry
    FROM Account
    WHERE Name LIKE 'BulkTest%'
];

for (Account acc : accounts) {
    acc.Description = 'Bulk updated on ' + DateTime.now();
}

update accounts;

System.debug('Updated ' + accounts.size() + ' accounts');
```

## Test Bulk Delete

```apex
// Delete in reverse order (children first)
List<Task> tasks = [SELECT Id FROM Task WHERE What.Name LIKE 'BulkTest%'];
delete tasks;

List<Contact> contacts = [SELECT Id FROM Contact WHERE Account.Name LIKE 'BulkTest%'];
delete contacts;

List<Account> accounts = [SELECT Id FROM Account WHERE Name LIKE 'BulkTest%'];
delete accounts;

System.debug('Cleanup complete');
```

## Governor Limits Monitoring

```apex
// Add to your test script to monitor limits
System.debug('=== GOVERNOR LIMITS ===');
System.debug('SOQL: ' + Limits.getQueries() + '/' + Limits.getLimitQueries());
System.debug('DML Statements: ' + Limits.getDmlStatements() + '/' + Limits.getLimitDmlStatements());
System.debug('DML Rows: ' + Limits.getDmlRows() + '/' + Limits.getLimitDmlRows());
System.debug('CPU Time: ' + Limits.getCpuTime() + 'ms/' + Limits.getLimitCpuTime() + 'ms');
System.debug('Heap: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
```

## Validation Score

```text
Score: 128/130 ⭐⭐⭐⭐⭐ Excellent
├─ Query Efficiency: 25/25 (bulk queries)
├─ Bulk Safety: 25/25 (251 records, limits monitored)
├─ Data Integrity: 20/20 (valid field values)
├─ Security & FLS: 20/20 (no sensitive data)
├─ Test Patterns: 15/15 (201+ records, variations)
├─ Cleanup & Isolation: 13/15 (cleanup script provided)
└─ Documentation: 10/10 (fully documented)
```

## Common Issues

| Issue | Cause | Solution |
|-------|-------|----------|
| `SOQL 101` | Query in loop | Use Map or Set for bulk queries |
| `DML 151` | DML in loop | Collect records, single DML |
| `CPU timeout` | Complex logic | Optimize loops, async processing |
| `Too many records` | >10,000 DML rows | Use Bulk API or Batch Apex |
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall