|
38 | 38 | public without sharing class HouseholdNamingService {
|
39 | 39 |
|
40 | 40 | private HouseholdSettings settings = new HouseholdSettings();
|
| 41 | + private static Boolean isNamingServiceExecuted = false; |
41 | 42 |
|
42 | 43 | @TestVisible
|
43 | 44 | private ContactSelector contactSelector {
|
@@ -419,12 +420,38 @@ public without sharing class HouseholdNamingService {
|
419 | 420 | }
|
420 | 421 |
|
421 | 422 | public void setCustomNamingField(List<SObject> records, Map<Id, SObject> oldMap) {
|
| 423 | + if (isNamingServiceExecuted) { |
| 424 | + return; |
| 425 | + } |
| 426 | + isNamingServiceExecuted = true; |
| 427 | + |
422 | 428 | for (SObject household : records) {
|
423 | 429 | 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 | + } |
425 | 436 | }
|
426 | 437 | }
|
427 | 438 |
|
| 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 | + |
428 | 455 | public void setNameAndGreetingsToReplacementText(List<SObject> records) {
|
429 | 456 | for (SObject household : records) {
|
430 | 457 | setNameAndGreetingsToReplacementText(household);
|
@@ -534,8 +561,13 @@ public without sharing class HouseholdNamingService {
|
534 | 561 | private void setCustomNamingStringValue(SObject household, SObject oldRecord) {
|
535 | 562 | HouseholdNamingUserControlledFields userControlledNamingFields =
|
536 | 563 | 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 | + } |
539 | 571 | }
|
540 | 572 |
|
541 | 573 | private String namingOverridesFor(SObject household) {
|
|
0 commit comments