sf-agent-framework
Version:
AI Agent Orchestration Framework for Salesforce Development - Two-phase architecture with 70% context reduction
431 lines (329 loc) • 12.9 kB
Markdown
Data quality is critical for successful Salesforce implementations. This
document outlines the six dimensions of data quality and provides practical
guidance for maintaining high-quality data in Salesforce orgs.
**Definition**: The extent to which all required data is present and populated.
```apex
// Calculate field completeness percentage
public static Decimal calculateCompleteness(String objectName, String fieldName) {
Integer totalRecords = Database.countQuery('SELECT COUNT() FROM ' + objectName);
Integer populatedRecords = Database.countQuery(
'SELECT COUNT() FROM ' + objectName + ' WHERE ' + fieldName + ' != null'
);
return totalRecords > 0 ? (Decimal)populatedRecords / totalRecords * 100 : 0;
}
```
- Make critical fields required at the database level
- Use validation rules for conditional requirements
- Implement page layouts with required fields
- Create data quality dashboards
**Definition**: The degree to which data correctly represents real-world
entities.
```apex
// Email validation
public static Boolean isValidEmail(String email) {
Pattern emailPattern = Pattern.compile(
'^[a-zA-Z0-9._|\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$'
);
return emailPattern.matcher(email).matches();
}
// Phone number standardization
public static String standardizePhone(String phone) {
// Remove all non-numeric characters
String cleaned = phone.replaceAll('[^0-9]', '');
// Format as (XXX) XXX-XXXX for US numbers
if (cleaned.length() == 10) {
return '(' + cleaned.substring(0,3) + ') ' +
cleaned.substring(3,6) + '-' +
cleaned.substring(6);
}
return phone;
}
```
**Definition**: The uniformity of data across different systems, objects, and
records.
```apex
// Cross-object consistency validation
public class DataConsistencyChecker {
public static List<InconsistencyResult> checkAccountContactConsistency() {
List<InconsistencyResult> results = new List<InconsistencyResult>();
// Find contacts with different address than their account
List<Contact> inconsistentContacts = [
SELECT Id, Name, MailingCity, Account.BillingCity
FROM Contact
WHERE AccountId != null
AND MailingCity != null
AND Account.BillingCity != null
AND MailingCity != Account.BillingCity
];
for (Contact c : inconsistentContacts) {
results.add(new InconsistencyResult(
'Contact/Account City Mismatch',
c.Id,
'Contact: ' + c.MailingCity + ', Account: ' + c.Account.BillingCity
));
}
return results;
}
}
```
**Definition**: Data is available when needed and reflects current state.
```apex
// Track data freshness
public class DataTimelinessMonitor {
public static Map<String, DateTime> getLastModifiedDates(String objectName) {
Map<String, DateTime> results = new Map<String, DateTime>();
// Get most recent modification
AggregateResult[] lastModified = [
SELECT MAX(LastModifiedDate) maxDate
FROM Account
];
results.put('lastModified', (DateTime)lastModified[0].get('maxDate'));
results.put('dataAge', DateTime.now());
return results;
}
// Alert on stale data
public static List<String> findStaleRecords(Integer daysThreshold) {
DateTime threshold = DateTime.now().addDays(-daysThreshold);
return [
SELECT Id FROM Opportunity
WHERE StageName NOT IN ('Closed Won', 'Closed Lost')
AND LastModifiedDate < :threshold
];
}
}
```
**Definition**: Data conforms to defined business rules and constraints.
```apex
// Business rule validation
public class DataValidityChecker {
public static ValidationResult validateOpportunityStage(Opportunity opp) {
ValidationResult result = new ValidationResult();
// Stage-specific validations
if (opp.StageName == 'Closed Won') {
if (opp.CloseDate == null) {
result.addError('Close Date is required for Closed Won');
}
if (opp.Amount == null || opp.Amount <= 0) {
result.addError('Amount must be greater than 0 for Closed Won');
}
}
// Probability alignment
Map<String, Decimal> stageProbability = new Map<String, Decimal>{
'Prospecting' => 10,
'Qualification' => 20,
'Needs Analysis' => 40,
'Value Proposition' => 60,
'Decision Makers' => 80,
'Closed Won' => 100,
'Closed Lost' => 0
};
if (stageProbability.containsKey(opp.StageName)) {
Decimal expectedProb = stageProbability.get(opp.StageName);
if (Math.abs(opp.Probability - expectedProb) > 10) {
result.addWarning('Probability doesn\'t match stage guidelines');
}
}
return result;
}
}
```
**Definition**: No unwanted duplication exists in the dataset.
```apex
// Duplicate detection algorithm
public class DuplicateDetector {
public static List<DuplicateSet> findDuplicateAccounts() {
Map<String, List<Account>> potentialDupes = new Map<String, List<Account>>();
// Group by normalized name
for (Account acc : [SELECT Id, Name, Website, Phone FROM Account]) {
String key = normalizeForMatching(acc.Name);
if (!potentialDupes.containsKey(key)) {
potentialDupes.put(key, new List<Account>());
}
potentialDupes.get(key).add(acc);
}
// Find groups with multiple records
List<DuplicateSet> duplicates = new List<DuplicateSet>();
for (String key : potentialDupes.keySet()) {
List<Account> accounts = potentialDupes.get(key);
if (accounts.size() > 1) {
duplicates.add(new DuplicateSet(key, accounts));
}
}
return duplicates;
}
private static String normalizeForMatching(String input) {
return input.toLowerCase()
.replaceAll('[^a-z0-9]', '')
.replaceAll('\\b(inc|incorporated|corp|corporation|llc|ltd)\\b', '');
}
}
```
```apex
public class DataQualityScorer {
public class QualityScore {
public Decimal completeness { get; set; }
public Decimal accuracy { get; set; }
public Decimal consistency { get; set; }
public Decimal timeliness { get; set; }
public Decimal validity { get; set; }
public Decimal uniqueness { get; set; }
public Decimal getOverallScore() {
return (completeness + accuracy + consistency +
timeliness + validity + uniqueness) / 6;
}
}
public static QualityScore scoreObject(String objectName) {
QualityScore score = new QualityScore();
// Calculate each dimension
score.completeness = calculateCompleteness(objectName);
score.accuracy = calculateAccuracy(objectName);
score.consistency = calculateConsistency(objectName);
score.timeliness = calculateTimeliness(objectName);
score.validity = calculateValidity(objectName);
score.uniqueness = calculateUniqueness(objectName);
return score;
}
}
```
- **Validation Rules**: Implement at field and object level
- **Duplicate Rules**: Configure matching and merge rules
- **Required Fields**: Use page layouts and field-level security
- **Picklists**: Standardize values with restricted picklists
- **Lookup Filters**: Ensure valid relationships
```apex
// Automated data quality monitoring
@Scheduled
public class DataQualityMonitor implements Schedulable {
public void execute(SchedulableContext ctx) {
// Run quality checks
DataQualityReport report = new DataQualityReport();
// Check each dimension
report.checkCompleteness();
report.checkAccuracy();
report.checkConsistency();
report.checkTimeliness();
report.checkValidity();
report.checkUniqueness();
// Send alerts if thresholds breached
if (report.hasIssues()) {
report.sendAlerts();
}
// Log metrics
report.logMetrics();
}
}
```
- **Data Cleansing**: Batch jobs to fix known issues
- **Enrichment**: Third-party data services
- **Standardization**: Format consistency
- **Deduplication**: Merge duplicate records
- [ ] Profile current data quality
- [ ] Identify critical data elements
- [ ] Define quality thresholds
- [ ] Document business rules
- [ ] Implement validation rules
- [ ] Configure duplicate management
- [ ] Set up required fields
- [ ] Create data entry guidelines
- [ ] Build quality dashboards
- [ ] Schedule quality reports
- [ ] Set up alerts
- [ ] Create quality metrics
- [ ] Develop cleansing procedures
- [ ] Create merge processes
- [ ] Implement enrichment
- [ ] Train users
1. **Data Loader**: Bulk updates and cleanups
2. **Duplicate Management**: Built-in deduplication
3. **Validation Rules**: Preventive controls
4. **Reports & Dashboards**: Quality monitoring
5. **Flow Builder**: Automated corrections
1. **Data Quality Services**: Dun & Bradstreet, ZoomInfo
2. **MDM Platforms**: Informatica, Talend
3. **Cleansing Tools**: Cloudingo, DemandTools
4. **Analytics**: Tableau CRM for quality insights
- **Data Stewards**: Own data quality for specific domains
- **System Administrators**: Implement technical controls
- **Business Users**: Follow data entry standards
- **Data Governance Team**: Define policies and standards
```apex
// Sample KPI tracking
public class DataQualityKPIs {
public static Map<String, Decimal> calculateKPIs() {
Map<String, Decimal> kpis = new Map<String, Decimal>();
// Overall completeness
kpis.put('Overall_Completeness', calculateOverallCompleteness());
// Duplicate rate
kpis.put('Duplicate_Rate', calculateDuplicateRate());
// Data freshness
kpis.put('Data_Freshness_Days', calculateAverageDataAge());
// Validation rule violations
kpis.put('Validation_Violations', countValidationViolations());
return kpis;
}
}
```
**Solution**: Progressive data capture
```apex
// Track and improve record completeness over time
trigger ProgressiveDataCapture on Account (before update) {
for (Account acc : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(acc.Id);
// Award points for completing fields
Integer completionScore = 0;
if (oldAcc.Website == null && acc.Website != null) completionScore += 10;
if (oldAcc.Industry == null && acc.Industry != null) completionScore += 10;
if (oldAcc.NumberOfEmployees == null && acc.NumberOfEmployees != null) completionScore += 5;
if (completionScore > 0) {
acc.Data_Quality_Score__c =
(acc.Data_Quality_Score__c != null ? acc.Data_Quality_Score__c : 0) + completionScore;
}
}
}
```
**Solution**: Matching rules and merge procedures
**Solution**: Standardization triggers and workflows
**Solution**: Automated refresh and archival processes
- [Salesforce Data Quality Best Practices](https://help.salesforce.com/s/articleView?id=sf.data_quality_best_practices.htm)
- [Trailhead: Data Quality](https://trailhead.salesforce.com/en/content/learn/modules/data_quality)
- [Duplicate Management Implementation Guide](https://help.salesforce.com/s/articleView?id=sf.duplicate_management_implementation.htm)
- [AppExchange Data Quality Apps](https://appexchange.salesforce.com/appxStore?type=Data%20Quality)