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

assets/cleanup/rollback-transaction.apex

View on GitHub
/**
 * ═══════════════════════════════════════════════════════════════════════════════
 * TRANSACTION ROLLBACK PATTERN
 * Using Database.Savepoint for test isolation
 * ═══════════════════════════════════════════════════════════════════════════════
 *
 * PURPOSE:
 * Create test data, run tests, then roll back all changes.
 * This ensures complete test isolation without leaving orphan data.
 *
 * HOW IT WORKS:
 * 1. Create a savepoint BEFORE creating any test data
 * 2. Create your test data
 * 3. Run your test/validation logic
 * 4. Roll back to the savepoint (deletes all created records)
 *
 * USE CASES:
 * • Unit testing with data setup
 * • Integration testing without pollution
 * • Manual testing with automatic cleanup
 * • Validation runs that shouldn't persist
 *
 * LIMITATIONS:
 * • Cannot roll back asynchronous operations
 * • Savepoints don't persist across transactions
 * • Maximum 5 savepoints per transaction
 *
 * ═══════════════════════════════════════════════════════════════════════════════
 */

// ═══════════════════════════════════════════════════════════════════════════════
// BASIC SAVEPOINT/ROLLBACK PATTERN
// ═══════════════════════════════════════════════════════════════════════════════

System.debug('═══════════════════════════════════════════════════════════════');
System.debug('TRANSACTION ROLLBACK DEMONSTRATION');
System.debug('═══════════════════════════════════════════════════════════════');

// Step 1: Create savepoint BEFORE any DML
Savepoint sp = Database.setSavepoint();
System.debug('✓ Savepoint created');

// Track what we create for verification
Set<Id> createdAccountIds = new Set<Id>();
Set<Id> createdContactIds = new Set<Id>();

try {
    // ═══════════════════════════════════════════════════════════════════════
    // Step 2: Create test data
    // ═══════════════════════════════════════════════════════════════════════

    System.debug('');
    System.debug('Creating test data...');

    // Create Accounts
    List<Account> testAccounts = new List<Account>();
    for (Integer i = 0; i < 10; i++) {
        testAccounts.add(new Account(
            Name = 'Rollback Test Account ' + i,
            Industry = 'Technology',
            Description = 'This record will be rolled back'
        ));
    }
    insert testAccounts;
    for (Account acc : testAccounts) {
        createdAccountIds.add(acc.Id);
    }
    System.debug('  Created ' + testAccounts.size() + ' Accounts');

    // Create Contacts
    List<Contact> testContacts = new List<Contact>();
    for (Account acc : testAccounts) {
        testContacts.add(new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            AccountId = acc.Id,
            Email = 'test@rollback.example.com'
        ));
    }
    insert testContacts;
    for (Contact con : testContacts) {
        createdContactIds.add(con.Id);
    }
    System.debug('  Created ' + testContacts.size() + ' Contacts');

    // ═══════════════════════════════════════════════════════════════════════
    // Step 3: Verify data exists
    // ═══════════════════════════════════════════════════════════════════════

    System.debug('');
    System.debug('Verifying data exists...');

    Integer accountCount = [SELECT COUNT() FROM Account WHERE Id IN :createdAccountIds];
    Integer contactCount = [SELECT COUNT() FROM Contact WHERE Id IN :createdContactIds];

    System.debug('  Accounts in database: ' + accountCount);
    System.debug('  Contacts in database: ' + contactCount);

    // ═══════════════════════════════════════════════════════════════════════
    // Step 4: Run your test logic here
    // ═══════════════════════════════════════════════════════════════════════

    System.debug('');
    System.debug('Running test logic...');

    // Example: Verify trigger fired correctly
    // Example: Check field calculations
    // Example: Validate workflow results

    System.debug('  Test logic completed');

} finally {
    // ═══════════════════════════════════════════════════════════════════════
    // Step 5: Roll back ALL changes
    // ═══════════════════════════════════════════════════════════════════════

    System.debug('');
    System.debug('Rolling back transaction...');

    Database.rollback(sp);

    System.debug('✓ Rollback complete');
}

// ═══════════════════════════════════════════════════════════════════════════════
// Step 6: Verify rollback worked
// ═══════════════════════════════════════════════════════════════════════════════

System.debug('');
System.debug('Verifying rollback...');

Integer accountCountAfter = [SELECT COUNT() FROM Account WHERE Id IN :createdAccountIds];
Integer contactCountAfter = [SELECT COUNT() FROM Contact WHERE Id IN :createdContactIds];

System.debug('  Accounts remaining: ' + accountCountAfter + ' (expected: 0)');
System.debug('  Contacts remaining: ' + contactCountAfter + ' (expected: 0)');

if (accountCountAfter == 0 && contactCountAfter == 0) {
    System.debug('');
    System.debug('═══════════════════════════════════════════════════════════════');
    System.debug('✅ ROLLBACK SUCCESSFUL - All test data removed');
    System.debug('═══════════════════════════════════════════════════════════════');
} else {
    System.debug('');
    System.debug('⚠️  WARNING: Some records were not rolled back');
}

// ═══════════════════════════════════════════════════════════════════════════════
// PATTERN: TEST METHOD WITH SAVEPOINT
// ═══════════════════════════════════════════════════════════════════════════════

/*
@isTest
public class MyTestClass {

    @isTest
    static void testWithRollback() {
        // In test methods, DML is automatically rolled back
        // This example shows explicit savepoint usage

        Savepoint sp = Database.setSavepoint();

        try {
            // Create test data
            Account testAccount = new Account(Name = 'Test');
            insert testAccount;

            // Run the test
            Test.startTest();
            MyClass.processAccount(testAccount.Id);
            Test.stopTest();

            // Assert results
            Account result = [SELECT Field__c FROM Account WHERE Id = :testAccount.Id];
            System.assertEquals('Expected', result.Field__c);

        } finally {
            // Optional explicit rollback (tests auto-rollback anyway)
            Database.rollback(sp);
        }
    }
}
*/

// ═══════════════════════════════════════════════════════════════════════════════
// PATTERN: NESTED SAVEPOINTS
// ═══════════════════════════════════════════════════════════════════════════════

/*
// You can have up to 5 savepoints

Savepoint sp1 = Database.setSavepoint();

// Create data set 1
insert accounts;

Savepoint sp2 = Database.setSavepoint();

// Create data set 2
insert contacts;

// Rollback to sp2 (removes contacts, keeps accounts)
Database.rollback(sp2);

// Rollback to sp1 (removes everything)
Database.rollback(sp1);
*/

// ═══════════════════════════════════════════════════════════════════════════════
// PATTERN: CONDITIONAL ROLLBACK
// ═══════════════════════════════════════════════════════════════════════════════

/*
Savepoint sp = Database.setSavepoint();
Boolean shouldCommit = true;

try {
    // Create data
    insert records;

    // Validate
    if (!isValid(records)) {
        shouldCommit = false;
    }

} finally {
    if (!shouldCommit) {
        Database.rollback(sp);
        System.debug('Changes rolled back due to validation failure');
    } else {
        System.debug('Changes committed');
    }
}
*/

// ═══════════════════════════════════════════════════════════════════════════════
// IMPORTANT NOTES
// ═══════════════════════════════════════════════════════════════════════════════

/*
1. SAVEPOINT LIMITATIONS:
   • Max 5 savepoints per transaction
   • Cannot cross transaction boundaries
   • Does not roll back @future, Queueable, or Batch operations
   • Does not roll back platform events
   • Does not roll back email sends

2. WHAT GETS ROLLED BACK:
   ✓ All DML operations (insert, update, delete, undelete)
   ✓ Workflow field updates triggered by DML
   ✓ Process Builder / Flow changes triggered by DML

3. WHAT DOES NOT GET ROLLED BACK:
   ✗ Asynchronous operations (@future, Queueable, Batch, Scheduled)
   ✗ Platform events
   ✗ Outbound messages
   ✗ Email sends
   ✗ Callouts (HTTP requests)
   ✗ Debug logs

4. BEST PRACTICES:
   • Create savepoint as early as possible
   • Always use try-finally to ensure rollback
   • Verify rollback worked by querying records
   • Don't rely on savepoints for async operations
*/
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