forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-data-manage
platform-data-manage
VerifiedSalesforce 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).
assets/factories/contact-factory.apex
View on GitHub/**
* Test Data Factory for Contact records
* Supports bulk creation with Account relationships
*
* Usage:
* List<Contact> contacts = TestDataFactory_Contact.create(200);
* List<Contact> contactsForAccount = TestDataFactory_Contact.createForAccount(50, accountId);
* List<Contact> contactsNoInsert = TestDataFactory_Contact.create(200, false);
*/
public class TestDataFactory_Contact {
private static final String DEFAULT_FIRST_NAME_PREFIX = 'Test';
private static final String DEFAULT_LAST_NAME_PREFIX = 'Contact';
private static final String DEFAULT_EMAIL_DOMAIN = 'testfactory.example.com';
/**
* Create and insert Contact records (creates parent Account automatically)
* @param count Number of records to create
* @return List of inserted Contact records with IDs
*/
public static List<Contact> create(Integer count) {
return create(count, true);
}
/**
* Create Contact records with insert option
* @param count Number of records to create
* @param doInsert Whether to insert records
* @return List of Contact records
*/
public static List<Contact> create(Integer count, Boolean doInsert) {
// Create a parent Account for orphan protection
Account parentAccount = new Account(Name = 'Test Parent Account for Contacts');
insert parentAccount;
return createForAccount(count, parentAccount.Id, doInsert);
}
/**
* Create Contact records for a specific Account
* @param count Number of records to create
* @param accountId Parent Account ID
* @return List of inserted Contact records
*/
public static List<Contact> createForAccount(Integer count, Id accountId) {
return createForAccount(count, accountId, true);
}
/**
* Create Contact records for a specific Account with insert option
* @param count Number of records to create
* @param accountId Parent Account ID
* @param doInsert Whether to insert records
* @return List of Contact records
*/
public static List<Contact> createForAccount(Integer count, Id accountId, Boolean doInsert) {
List<Contact> records = new List<Contact>();
for (Integer i = 0; i < count; i++) {
records.add(buildRecord(i, accountId));
}
if (doInsert && !records.isEmpty()) {
insert records;
}
return records;
}
/**
* Create Contacts distributed across multiple Accounts
* @param accountIds List of Account IDs
* @param contactsPerAccount Number of Contacts per Account
* @return List of inserted Contact records
*/
public static List<Contact> createForAccounts(List<Id> accountIds, Integer contactsPerAccount) {
List<Contact> records = new List<Contact>();
Integer globalIndex = 0;
for (Id accountId : accountIds) {
for (Integer i = 0; i < contactsPerAccount; i++) {
records.add(buildRecord(globalIndex++, accountId));
}
}
if (!records.isEmpty()) {
insert records;
}
return records;
}
/**
* Create Contacts with varied titles for role-based testing
* @param count Number of records to create
* @param accountId Parent Account ID
* @return List of inserted Contact records with varied titles
*/
public static List<Contact> createWithVariedTitles(Integer count, Id accountId) {
List<String> titles = new List<String>{
'CEO', 'CFO', 'CTO', 'VP Sales', 'VP Marketing',
'Director', 'Manager', 'Senior Developer', 'Analyst', 'Consultant'
};
List<Contact> records = new List<Contact>();
for (Integer i = 0; i < count; i++) {
Contact con = buildRecord(i, accountId);
con.Title = titles[Math.mod(i, titles.size())];
records.add(con);
}
if (!records.isEmpty()) {
insert records;
}
return records;
}
/**
* Build a single Contact record with default values
* @param index Record index for unique naming
* @param accountId Parent Account ID
* @return Contact record (not inserted)
*/
private static Contact buildRecord(Integer index, Id accountId) {
String indexStr = String.valueOf(index).leftPad(5, '0');
String firstName = DEFAULT_FIRST_NAME_PREFIX + indexStr;
String lastName = DEFAULT_LAST_NAME_PREFIX + indexStr;
return new Contact(
FirstName = firstName,
LastName = lastName,
AccountId = accountId,
Email = firstName.toLowerCase() + '.' + lastName.toLowerCase() + '@' + DEFAULT_EMAIL_DOMAIN,
Phone = '(555) 200-' + String.valueOf(1000 + index),
MobilePhone = '(555) 300-' + String.valueOf(1000 + index),
Title = 'Test Title',
Department = 'Test Department',
MailingStreet = '456 Contact Ave',
MailingCity = 'San Francisco',
MailingState = 'CA',
MailingPostalCode = '94103',
MailingCountry = 'USA',
Birthdate = Date.today().addYears(-30).addDays(index),
Description = 'Test contact created by TestDataFactory for automated testing'
);
}
/**
* Get IDs of created records for cleanup
* @param records List of Contact records
* @return Set of Contact IDs
*/
public static Set<Id> getIds(List<Contact> records) {
Set<Id> ids = new Set<Id>();
for (Contact con : records) {
if (con.Id != null) {
ids.add(con.Id);
}
}
return ids;
}
/**
* Delete records by IDs (cleanup utility)
* @param recordIds Set of Contact IDs to delete
*/
public static void cleanup(Set<Id> recordIds) {
if (!recordIds.isEmpty()) {
delete [SELECT Id FROM Contact WHERE Id IN :recordIds];
}
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| 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: 198k | Updated | Claude CodeCodex | Verified | Install |