Skip to content

Commit e12ba77

Browse files
authored
Merge pull request #7277 from SalesforceFoundation/feature/256__W-15103158-fix-incorrectly-updated-System_Custom_Naming-field
W-15103158 - Fix incorrect updates to the System_Custom_Naming field
2 parents d8879fe + c83c8cc commit e12ba77

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

force-app/main/service/HouseholdNamingService.cls

+35-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
public without sharing class HouseholdNamingService {
3939

4040
private HouseholdSettings settings = new HouseholdSettings();
41+
private static Boolean isNamingServiceExecuted = false;
4142

4243
@TestVisible
4344
private ContactSelector contactSelector {
@@ -419,12 +420,38 @@ public without sharing class HouseholdNamingService {
419420
}
420421

421422
public void setCustomNamingField(List<SObject> records, Map<Id, SObject> oldMap) {
423+
if (isNamingServiceExecuted) {
424+
return;
425+
}
426+
isNamingServiceExecuted = true;
427+
422428
for (SObject household : records) {
423429
SObject oldRecord = oldMap.get(household.Id);
424-
setCustomNamingStringValue(household, oldRecord);
430+
// Update only if manual changes are detected in the relevant fields
431+
if (manualChangesDetected(household, oldRecord)) {
432+
setCustomNamingStringValue(household, oldRecord); // Populate the custom naming field
433+
} else {
434+
household.put('npo02__SYSTEM_CUSTOM_NAMING__c', null);
435+
}
425436
}
426437
}
427438

439+
private Boolean manualChangesDetected(SObject household, SObject oldRecord) {
440+
// Detect changes in fields that impact the custom naming logic
441+
return hasFieldChanged(household, oldRecord, 'npo02__Formal_Greeting__c') ||
442+
hasFieldChanged(household, oldRecord, 'npo02__Informal_Greeting__c') ||
443+
hasFieldChanged(household, oldRecord, 'Name');
444+
}
445+
446+
private Boolean hasFieldChanged(SObject current, SObject oldRecord, String fieldApiName) {
447+
if (oldRecord == null) {
448+
return true; // Treat as changed if no old record exists
449+
}
450+
Object currentValue = current.get(fieldApiName);
451+
Object oldValue = oldRecord.get(fieldApiName);
452+
return currentValue != oldValue; // Compare values for change
453+
}
454+
428455
public void setNameAndGreetingsToReplacementText(List<SObject> records) {
429456
for (SObject household : records) {
430457
setNameAndGreetingsToReplacementText(household);
@@ -534,8 +561,13 @@ public without sharing class HouseholdNamingService {
534561
private void setCustomNamingStringValue(SObject household, SObject oldRecord) {
535562
HouseholdNamingUserControlledFields userControlledNamingFields =
536563
new HouseholdNamingUserControlledFields(household, oldRecord);
537-
household.put('npo02__SYSTEM_CUSTOM_NAMING__c',
538-
userControlledNamingFields.asConcatenatedString());
564+
String concatenatedValue = userControlledNamingFields.asConcatenatedString();
565+
566+
if (!String.isBlank(concatenatedValue)) {
567+
household.put('npo02__SYSTEM_CUSTOM_NAMING__c', concatenatedValue); // Populate field
568+
} else {
569+
household.put('npo02__SYSTEM_CUSTOM_NAMING__c', null); // Clear the field if no value is needed
570+
}
539571
}
540572

541573
private String namingOverridesFor(SObject household) {

0 commit comments

Comments
 (0)