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/factories/user-factory.apex

View on GitHub
/**
 * Test Data Factory for User records
 * Supports creation of test users with various profiles and roles
 *
 * NOTE: User creation requires specific permissions and may fail in some orgs.
 * Users created in tests are automatically rolled back.
 *
 * Usage:
 *   User testUser = TestDataFactory_User.createStandardUser();
 *   User adminUser = TestDataFactory_User.createSystemAdmin();
 *   List<User> users = TestDataFactory_User.create(5, 'Standard User');
 */
public class TestDataFactory_User {

    private static final String DEFAULT_EMAIL_DOMAIN = 'testuser.example.com';
    private static final String DEFAULT_LOCALE = 'en_US';
    private static final String DEFAULT_LANGUAGE = 'en_US';
    private static final String DEFAULT_TIMEZONE = 'America/Los_Angeles';
    private static final String DEFAULT_ENCODING = 'UTF-8';

    /**
     * Create a single Standard User
     * @return Inserted User record
     */
    public static User createStandardUser() {
        return createWithProfile('Standard User');
    }

    /**
     * Create a single System Administrator
     * @return Inserted User record
     */
    public static User createSystemAdmin() {
        return createWithProfile('System Administrator');
    }

    /**
     * Create a single User with a specific Profile
     * @param profileName Profile name
     * @return Inserted User record
     */
    public static User createWithProfile(String profileName) {
        List<User> users = create(1, profileName);
        return users.isEmpty() ? null : users[0];
    }

    /**
     * Create multiple Users with a specific Profile
     * @param count Number of users to create
     * @param profileName Profile name
     * @return List of inserted User records
     */
    public static List<User> create(Integer count, String profileName) {
        return create(count, profileName, true);
    }

    /**
     * Create multiple Users with insert option
     * @param count Number of users to create
     * @param profileName Profile name
     * @param doInsert Whether to insert records
     * @return List of User records
     */
    public static List<User> create(Integer count, String profileName, Boolean doInsert) {
        // Query the Profile
        List<Profile> profiles = [
            SELECT Id, Name
            FROM Profile
            WHERE Name = :profileName
            LIMIT 1
        ];

        if (profiles.isEmpty()) {
            throw new TestDataFactoryException('Profile not found: ' + profileName);
        }

        Profile p = profiles[0];

        List<User> records = new List<User>();
        for (Integer i = 0; i < count; i++) {
            records.add(buildRecord(i, p.Id));
        }

        if (doInsert && !records.isEmpty()) {
            insert records;
        }
        return records;
    }

    /**
     * Create Users with a specific Role
     * @param count Number of users to create
     * @param profileName Profile name
     * @param roleName Role DeveloperName
     * @return List of inserted User records
     */
    public static List<User> createWithRole(Integer count, String profileName, String roleName) {
        // Query the Profile
        List<Profile> profiles = [
            SELECT Id FROM Profile WHERE Name = :profileName LIMIT 1
        ];
        if (profiles.isEmpty()) {
            throw new TestDataFactoryException('Profile not found: ' + profileName);
        }

        // Query the Role
        List<UserRole> roles = [
            SELECT Id FROM UserRole WHERE DeveloperName = :roleName LIMIT 1
        ];
        if (roles.isEmpty()) {
            throw new TestDataFactoryException('Role not found: ' + roleName);
        }

        List<User> records = new List<User>();
        for (Integer i = 0; i < count; i++) {
            User u = buildRecord(i, profiles[0].Id);
            u.UserRoleId = roles[0].Id;
            records.add(u);
        }

        if (!records.isEmpty()) {
            insert records;
        }
        return records;
    }

    /**
     * Create a Community/Experience Cloud User
     * @param contactId Contact ID for the community user
     * @param communityProfileName Community profile name
     * @return Inserted community User record
     */
    public static User createCommunityUser(Id contactId, String communityProfileName) {
        // Query the Profile
        List<Profile> profiles = [
            SELECT Id FROM Profile WHERE Name = :communityProfileName LIMIT 1
        ];
        if (profiles.isEmpty()) {
            throw new TestDataFactoryException('Community Profile not found: ' + communityProfileName);
        }

        // Get Contact details
        Contact con = [
            SELECT Id, FirstName, LastName, Email, AccountId
            FROM Contact
            WHERE Id = :contactId
            LIMIT 1
        ];

        String uniqueId = String.valueOf(DateTime.now().getTime());
        String alias = (con.FirstName != null ? con.FirstName.substring(0, 1) : 'X') +
                       con.LastName.substring(0, Math.min(4, con.LastName.length()));

        User u = new User(
            FirstName = con.FirstName,
            LastName = con.LastName,
            Email = con.Email,
            Username = 'community.' + uniqueId + '@' + DEFAULT_EMAIL_DOMAIN,
            Alias = alias.toLowerCase(),
            ProfileId = profiles[0].Id,
            ContactId = contactId,
            LocaleSidKey = DEFAULT_LOCALE,
            LanguageLocaleKey = DEFAULT_LANGUAGE,
            TimeZoneSidKey = DEFAULT_TIMEZONE,
            EmailEncodingKey = DEFAULT_ENCODING,
            IsActive = true
        );

        insert u;
        return u;
    }

    /**
     * Create an inactive User for historical/deactivated user testing
     * @param profileName Profile name
     * @return Inserted inactive User record
     */
    public static User createInactiveUser(String profileName) {
        User u = createWithProfile(profileName);
        u.IsActive = false;
        update u;
        return u;
    }

    /**
     * Build a single User record with default values
     * @param index Record index for unique naming
     * @param profileId Profile ID
     * @return User record (not inserted)
     */
    private static User buildRecord(Integer index, Id profileId) {
        String uniqueId = String.valueOf(DateTime.now().getTime()) + String.valueOf(index);
        String indexStr = String.valueOf(index).leftPad(5, '0');

        return new User(
            FirstName = 'Test',
            LastName = 'User' + indexStr,
            Email = 'testuser' + uniqueId + '@' + DEFAULT_EMAIL_DOMAIN,
            Username = 'testuser' + uniqueId + '@' + DEFAULT_EMAIL_DOMAIN,
            Alias = 'tuser' + index,
            ProfileId = profileId,
            LocaleSidKey = DEFAULT_LOCALE,
            LanguageLocaleKey = DEFAULT_LANGUAGE,
            TimeZoneSidKey = DEFAULT_TIMEZONE,
            EmailEncodingKey = DEFAULT_ENCODING,
            IsActive = true,
            Street = '123 Test User Street',
            City = 'San Francisco',
            State = 'CA',
            PostalCode = '94105',
            Country = 'USA',
            Phone = '(555) 700-' + String.valueOf(1000 + index),
            MobilePhone = '(555) 800-' + String.valueOf(1000 + index),
            Title = 'Test User',
            Department = 'Test Department',
            CompanyName = 'Test Company'
        );
    }

    /**
     * Run code as a specific user (for testing user context)
     * @param userId User ID to run as
     * @param callback Runnable code to execute
     */
    public static void runAs(Id userId, Runnable callback) {
        User u = [SELECT Id FROM User WHERE Id = :userId LIMIT 1];
        System.runAs(u) {
            callback.run();
        }
    }

    /**
     * Interface for runnable callback
     */
    public interface Runnable {
        void run();
    }

    /**
     * Get IDs of created records for cleanup
     * @param records List of User records
     * @return Set of User IDs
     */
    public static Set<Id> getIds(List<User> records) {
        Set<Id> ids = new Set<Id>();
        for (User u : records) {
            if (u.Id != null) {
                ids.add(u.Id);
            }
        }
        return ids;
    }

    /**
     * Deactivate users (cannot delete Users)
     * @param recordIds Set of User IDs to deactivate
     */
    public static void cleanup(Set<Id> recordIds) {
        if (!recordIds.isEmpty()) {
            List<User> usersToDeactivate = [
                SELECT Id, IsActive
                FROM User
                WHERE Id IN :recordIds AND IsActive = true
            ];
            for (User u : usersToDeactivate) {
                u.IsActive = false;
            }
            if (!usersToDeactivate.isEmpty()) {
                update usersToDeactivate;
            }
        }
    }

    /**
     * Custom exception for factory errors
     */
    public class TestDataFactoryException extends Exception {}
}
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 323kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 323kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 323kUpdated 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