Skip to content

Commit

Permalink
Changes made in BDI_DataImportService.cls to update contact fields
Browse files Browse the repository at this point in the history
Code added in BDI_DataImportService.cls to ensure all relevant fields of contact are updated
  • Loading branch information
salesforce-suyash-more committed Jul 31, 2024
1 parent 8c8211b commit 6280c9b
Showing 1 changed file with 67 additions and 7 deletions.
74 changes: 67 additions & 7 deletions force-app/main/default/classes/BDI_DataImportService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1333,18 +1333,39 @@ global with sharing class BDI_DataImportService {
dataImport.Account1Imported__c = acc.Id;
}
}
// set c1's primary affilation
// Set C1's fields first
if (dataImport.Account1Imported__c != null) {
Contact c1 = ContactFromDi(dataImport, 1);
if (c1 != null && c1.Primary_Affiliation__c != dataImport.Account1Imported__c) {
c1.Primary_Affiliation__c = dataImport.Account1Imported__c;
if (c1 != null) {
updateContactFieldsForImport(c1, dataImport, 'Contact1_');
if (mapConIdToConUpdate.get(c1.Id) == null) {
mapConIdToConUpdate.put(c1.Id, c1);
}
}
}
}

// Update contacts to ensure all relevant fields are updated
if (mapConIdToConUpdate.size() > 0) {
UTIL_DMLService.updateRecords(mapConIdToConUpdate.values());
}

// Set C1's primary affiliation separately to avoid conflicts
for (DataImport__c dataImport : listDI) {
if (dataImport.Account1Imported__c != null) {
Contact c1 = ContactFromDi(dataImport, 1);
if (c1 != null && c1.Primary_Affiliation__c != dataImport.Account1Imported__c) {
c1.Primary_Affiliation__c = dataImport.Account1Imported__c;
mapConIdToConUpdate.put(c1.Id, c1);
}
}
}

// Now update the Contacts again to set their Affiliations
if (mapConIdToConUpdate.size() > 0) {
UTIL_DMLService.updateRecords(mapConIdToConUpdate.values());
}

// create/update our A2's
listAccUpsert.clear();
listDIUpsert.clear();
Expand Down Expand Up @@ -1403,24 +1424,63 @@ global with sharing class BDI_DataImportService {
dataImport.Account2Imported__c = acc.Id;
}
}
// set c2's primary affilation
// Set C2's fields first
if (dataImport.Account2Imported__c != null) {
Contact c2 = ContactFromDi(dataImport, 2);
if (c2 != null && c2.Primary_Affiliation__c != dataImport.Account2Imported__c) {
c2.Primary_Affiliation__c = dataImport.Account2Imported__c;
if (c2 != null) {
updateContactFieldsForImport(c2, dataImport, 'Contact2_');
if (mapConIdToConUpdate.get(c2.Id) == null) {
mapConIdToConUpdate.put(c2.Id, c2);
}
}
}
}

// now update the Contacts to create their Affiliations
// Update contacts to ensure all relevant fields are updated
if (mapConIdToConUpdate.size() > 0) {
UTIL_DMLService.updateRecords(mapConIdToConUpdate.values());
}

// Set C2's primary affiliation separately to avoid conflicts
for (DataImport__c dataImport : listDI) {
if (dataImport.Account2Imported__c != null) {
Contact c2 = ContactFromDi(dataImport, 2);
if (c2 != null && c2.Primary_Affiliation__c != dataImport.Account2Imported__c) {
c2.Primary_Affiliation__c = dataImport.Account2Imported__c;
mapConIdToConUpdate.put(c2.Id, c2);
}
}
}
// now update the Contacts again to set their Affiliations
if (mapConIdToConUpdate.size() > 0) {
UTIL_DMLService.updateRecords(mapConIdToConUpdate.values());
}
}

private void updateContactFieldsForImport(Contact contact, DataImport__c dataImport, String contactPrefix) {
if (dataImport.get(contactPrefix + 'Home_Phone__c') != null) {
contact.HomePhone = (String) dataImport.get(contactPrefix + 'Home_Phone__c');
}
if (dataImport.get(contactPrefix + 'Work_Phone__c') != null) {
contact.Phone = (String) dataImport.get(contactPrefix + 'Work_Phone__c');
}
if (dataImport.get(contactPrefix + 'Mobile_Phone__c') != null) {
contact.MobilePhone = (String) dataImport.get(contactPrefix + 'Mobile_Phone__c');
}
if (dataImport.get(contactPrefix + 'Other_Phone__c') != null) {
contact.OtherPhone = (String) dataImport.get(contactPrefix + 'Other_Phone__c');
}
if (dataImport.get(contactPrefix + 'Personal_Email__c') != null) {
contact.Email = (String) dataImport.get(contactPrefix + 'Personal_Email__c');
}
if (dataImport.get(contactPrefix + 'Work_Email__c') != null) {
contact.npe01__WorkEmail__c = (String) dataImport.get(contactPrefix + 'Work_Email__c');
}
if (dataImport.get(contactPrefix + 'Alternate_Email__c') != null) {
contact.npe01__AlternateEmail__c = (String) dataImport.get(contactPrefix + 'Alternate_Email__c');
}
}

/*******************************************************************************************************
* @description returns the field name of the Account CustomID field for Account1 or Account2 in the
* Data Import object.
Expand Down

0 comments on commit 6280c9b

Please sign in to comment.