• AbhishekJ
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 8
    Replies
How can I alert users that they have added duplicate team members on an account.
Logic to dedupe the team members is already there, just need to let the users know that the duplicate that they added has been removed
<apex:page standardController="Account" extensions="ManageCompanyTeamMember" lightningStylesheets="true" showQuickActionVfHeader="false">
    <apex:slds />
    
    <script>
        function showLoading(){
            document.getElementById('loading-div').style.display = "block";
        }
    </script>
    
    <apex:sectionHeader title="Team Members" subtitle="{!Account.Name}"/>
    
    <apex:form id="teammemberPanel" >
        
        <div class="slds-scope" id="loading-div" style="display:none;">
            <div id="spinner" class="slds-spinner_container slds-is-absolute" >
                <div role="status" class="slds-spinner slds-spinner--large slds-spinner--brand">
                    <div class="slds-spinner__dot-a"></div>
                    <div class="slds-spinner__dot-b"></div>
                </div>
            </div>
        </div>
        
        <apex:variable value="{!0}" var="row"/>
        
        <apex:pageBlock title="">
            <apex:pageBlockButtons >
                <div align="center">
                    <apex:commandButton value="Add" action="{!add}" reRender="teammemberPanel" immediate="true"  style="margin:3px; background-color: #1b5297; color: white"/>
                    <apex:commandButton value="Save" action="{!save}" disabled="{!teammember.empty}" onclick="showLoading()"
                                        style="margin:3px; background-color: #1b5297; color: white"/>
                    <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"  style="margin:3px; background-color: #1b5297; color: white"/>
                </div>
            </apex:pageBlockButtons>
            
            <apex:panelGrid styleClass="list" cellspacing="0" cellpadding="0" border="0" rendered="{!teammember.empty}">
                <apex:outputText value="No records to display"/>
            </apex:panelGrid>
            
            <apex:pageBlockTable value="{!teammember}" var="f" rendered="{!NOT(teammember.empty)}">
                <apex:column headerValue="Action" styleClass="actionColumn">
                    <apex:commandLink value="X" action="{!remove}" styleClass="actionLink" reRender="teammemberPanel">
                        <apex:param name="row" value="{!row}" />
                    </apex:commandLink>
                    <apex:variable value="{!row+1}" var="row"/>
                </apex:column>
                
                
                <apex:column headerValue="Team Member">
                    <apex:inputField value="{!f.Team_Member__c}" style="width: 0px;"/>
                </apex:column>
                
                
                <apex:column headerValue="Sort Order">
                    <apex:inputField value="{!f.Weekly_Update_Sort_Order__c}" style="width: 80px;"/>
                </apex:column>
                
            </apex:pageBlockTable>  
            
        </apex:pageBlock>
        <!--
    <c:Typeahead object="User" placeHolder="Search For User" searchScope="NAME" styleClass="search"  />  -->
    </apex:form>
</apex:page>
 
public class ManageCompanyTeamMember {
    public transient String dmessage { get; set; }
    public List<Company_Team_Member__c> teammember { get; private set; }
    public List<Company_Team_Member__c> deleteTeammember { get; private set; }
    private Id accountId;

    public ManageCompanyTeamMember(ApexPages.StandardController controller) {
        accountId = controller.getRecord().Id;
        refresh();
        
    }

    public void refresh() {
        teammember = new List<Company_Team_Member__c>();
        deleteTeammember = new List<Company_Team_Member__c>();
        for (Company_Team_Member__c f : [SELECT Id, Name, Company__c, Team_Member__c, Weekly_Update_Sort_Order__c FROM Company_Team_Member__c WHERE Company__c=:accountId]) {
            teammember.add(f);
        }
        for (Integer i = 0; i < 1; i++) {
            add();
        }
    }

    public void add() {
        teammember.add(new Company_Team_Member__c(
            Company__c = accountId
        ));
    }
    
    public void remove() {
        try {
            Integer row = Decimal.valueOf(ApexPages.currentPage().getParameters().get('row')).intValue();
            Company_Team_Member__c cf = teammember.remove(row);            
            if (cf.Id != null) {
                deleteTeammember.add(cf);                
            }
        } catch (Exception e) {
        }        
    }
    
    public PageReference save() {
        
        Set<Id> uniqueMemberId = new Set<Id>();
        
        PageReference ref = null;
        List<Company_Team_Member__c> teammemberList = new List<Company_Team_Member__c>();
        for (Company_Team_Member__c t : teammember) {
              if (t.Team_Member__c != null && uniqueMemberId.add( t.Team_Member__c  )) {
                teammemberList.add(t);
            }            
        } 
        if (!deleteTeammember.isEmpty()) {
            Database.delete(deleteTeammember, false);
        }   
         if (!teammemberList.isEmpty()) {           
            teammember = teammemberList;            
            upsert teammember;
            refresh();
        } 
        return new PageReference('/' + accountId);
    }
}

 
I am getting NullPointerException error while saving a new contact record.
Here is the exact error message:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()

I am new to apex coding and this code isn't written by me, so finding it hard to troubleshoot the issue. Would anyone be able to help?
Here is Apex Trigger:
trigger ContactTrigger on Contact (before delete, after insert, after update,after delete,after undelete) { 
    if(Trigger.isAfter) {
            if(Trigger.isUpdate) {
            ContactTriggerHandler.onAfterUpdate(Trigger.new,Trigger.oldMap);
            }
            
            if(Trigger.isInsert){
             ContactTriggerHandler.OnAfterInsert(Trigger.new);
            }
            
            if(Trigger.IsDelete){
                ContactTriggerHandler.OnAfterDelete(Trigger.old);
            }
            
            if(Trigger.IsUnDelete){
                ContactTriggerHandler.OnUndelete(Trigger.new);
            }
    }
    if(Trigger.isBefore){
            if(Trigger.IsDelete){
            }
    }
}

And here is the Apex Class:
public class ContactTriggerHandler {
    
    public static void onAfterUpdate(List<Contact> newContacts, Map<id,Contact> oldContacts) {
            updateSortedCorrespondencePreferneceData(newContacts,oldContacts);
            updateSalutationOnCommunicationContent(newContacts,oldContacts); 
            id cpCapitalPartnerRT = fetchRecordTypeId('Account','CP Capital Partner');
            List<Contact> newList = new List<Contact>();
            
            set<Id> oldAndNewAccountIds = new set<id>();
            for(Contact con : newContacts) {
                oldAndNewAccountIds.add(con.accountId);
                oldAndNewAccountIds.add(oldContacts.get(con.id).accountId);
            }
            
            Map<Id,Account> accountDetails = new Map<Id,Account>([Select Id,RecordTypeId from Account where Id in :oldAndNewAccountIds]);
            for(Contact con : newContacts) {
                    if(accountDetails !=NULL && con!= Null && con.accountId != oldContacts.get(con.Id).accountId  ) { 
                        if(  accountDetails.containsKey(con.accountId) && (accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT || accountDetails.get(oldContacts.get(con.Id).accountId).recordTypeId == cpCapitalPartnerRT) ) {
                            newList.add(con);
                        }

                    } else if(accountDetails !=NULL && con!= Null && accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT ) {  
                        newList.add(con);
                    }
            }        
           
            Email_InformationClass.getEmailTableData(newList,oldContacts); 
			
            updateCPIntralinksAccess(newContacts,oldContacts);
			
    }
    
	
    public static void OnAfterDelete(contact[] oldList){
        
        updateCPIntralinksAccess(oldList, null);
    }
    
     public static void OnAfterInsert(contact[] newList){
        
        updateCPIntralinksAccess(newList, null);
    }
     
    public static void OnUndelete(contact[] newList){ 
         
         updateCPIntralinksAccess(newList,null);
    }
	
    private static void updateSalutationOnCommunicationContent(List<Contact> newContacts, Map<id,Contact> oldContacts){
        set<id> contactsToProcess = new set<id>();

        for(Contact con : newContacts) {
		
            if((String.isNotBlank(con.email) && !con.email.contains('@generalatlantic.com'))  && ((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
                                                                                                  (con.FirstName != oldContacts.get(con.id).FirstName)) || (con.International_Salutation_Type__c!= oldContacts.get(con.Id).International_Salutation_Type__c || String.isBlank(oldContacts.get(con.Id).email))
              )
			  
            {
                
                        contactsToProcess.add(con.id);
            }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Company__c, RecordType.Name from Correspondence_Preference__c where Recipient_Type__c = 'To' AND Contact__c In :contactsToProcess]);
            set<string> cpNames = new Set<String>();
            set<string> accountNames = new Set<String>();
            for(Correspondence_Preference__c cp : corrPrefToProcess) {
                cpNames.add(cp.RecordType.Name);
                accountNames.add(cp.Company__c);
            }
            List<Communications_Content__c> ccList = new List<Communications_Content__c>([Select Id,Salutation__c,Company__c,Correspondence_Preference__c from Communications_Content__c where Company__c IN :accountNames AND Correspondence_Preference__c IN :cpNames]);
            CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,true);
            handler.updateSalutationField(null,ccList); 
        }
		
    private static void updateSortedCorrespondencePreferneceData(List<Contact> newContacts, Map<id,Contact> oldContacts) {
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
               (con.Email != oldContacts.get(con.id).Email)
              ) {
                  contactsToProcess.add(con.id);
              }
        }
        
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Commitment__c, RecordTypeId from Correspondence_Preference__c where Contact__c In :contactsToProcess]);
            
            CorrespondenceTriggerHandler.getCommRecTypeData(corrPrefToProcess,null,false);
        }
    }
	
    private static id fetchRecordTypeId(string sObj, string rt) {
        System.debug('fetch params>>'+sObj + '<<<rt>>>'+rt);
        String recordtypeId = Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId(); 
        return Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId();
		
    }
	
    public static void updateCPIntralinksAccess(List<Contact> newContacts, Map<id,Contact> OldMap){
        
        Map<ID,List<contact>> mapConWithAcc = new Map<ID,List<contact>>();
        List<account> accList = new List<account>();
        String temp= '';
        Map<Id,String> accUpdateMap = new Map<Id,String>();
        Set<ID> accid = new Set<ID>();
        
        for(contact con: newContacts){
            if(oldMap == null || (oldMap.get(con.id).CP_Intralinks_Access__c != con.CP_Intralinks_Access__c)){ 
                accid.add(con.accountid); 
            }
            if(oldMap.get(con.Id).AccountId != con.AccountId)
            {
               accid.add(con.AccountId);
               accid.add(oldMap.get(con.Id).AccountId);
            }
        }
        

		
        for(Account acc : [Select id,CP_Intralinks_Access__c,(Select id,CP_Intralinks_Access__c,accountid from contacts) from account where id in :accid]) {
            set<string> val = new set<string>();
            String finalVal = '' ;
            for(Contact ct : acc.contacts) {
                if(String.isNOtBlank(ct.CP_Intralinks_Access__c)) {
                    val.addAll(ct.CP_Intralinks_Access__c.split(';')); 
                }
            }
            List<String> valList = new List<String>();
            valList.addAll(val);
            if(valList.size() > 0) {
                finalVal = String.join(valList,',');
            }
            acc.CP_Intralinks_Access__c = finalVal ;
            accList.add(acc);
        }
        if(accList.size() > 0) {
            update accList ;
        }
    }
}

 
I am receiving error email from ApexApplication <info@salesforce.com> with following content:
Subject: Developer script exception from ABC : ContactTrigger : ContactTrigger: execution of AfterUpdate  caused by: System.NullPointerException: Attempt to de-reference a null object  ()
Apex script unhandled trigger exception by user/organization: 

ContactTrigger: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.ContactTriggerHandler.onAfterUpdate: line 28, column 1
Trigger.ContactTrigger: line 15, column 1

Here is the Trigger:
trigger ContactTrigger on Contact (after insert, after update,after delete,after undelete) 
    
    if(Trigger.isAfter) {
            if(Trigger.isUpdate) {
            ContactTriggerHandler.onAfterUpdate(Trigger.new,Trigger.oldMap);
            }
            
            if(Trigger.isInsert){
             ContactTriggerHandler.OnAfterInsert(Trigger.new);
            }
            
            if(Trigger.IsDelete){
                ContactTriggerHandler.OnAfterDelete(Trigger.old);
            }
            
            if(Trigger.IsUnDelete){
                ContactTriggerHandler.OnUndelete(Trigger.new);
            }
    }
     
}

Here is apex class handler
public class ContactTriggerHandler {
    public static void onAfterUpdate(List<Contact> newContacts, Map<id,Contact> oldContacts) {
            updateSortedCorrespondencePreferneceData(newContacts,oldContacts);
            updateSalutationOnCommunicationContent(newContacts,oldContacts); 
            id cpCapitalPartnerRT = fetchRecordTypeId('Account','CP Capital Partner');
            List<Contact> newList = new List<Contact>();
            set<Id> oldAndNewAccountIds = new set<id>();
            for(Contact con : newContacts) {
                oldAndNewAccountIds.add(con.accountId);
                oldAndNewAccountIds.add(oldContacts.get(con.id).accountId);
            }
            Map<Id,Account> accountDetails = new Map<Id,Account>([Select Id,RecordTypeId from Account where Id in :oldAndNewAccountIds]);
            for(Contact con : newContacts) {
                    if(accountDetails !=NULL && con!= Null && con.accountId != oldContacts.get(con.Id).accountId  ) { 
                        if(  accountDetails.containsKey(con.accountId) && (accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT || accountDetails.get(oldContacts.get(con.Id).accountId).recordTypeId == cpCapitalPartnerRT) ) {
                            newList.add(con);
                        }
                    } else if(accountDetails !=NULL && con!= Null && accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT ) { 
                        newList.add(con);
                    }
            }        
            Email_InformationClass.getEmailTableData(newList,oldContacts); 
            updateCPIntralinksAccess(newContacts,oldContacts);
    }
    public static void OnAfterDelete(contact[] oldList){
        
        updateCPIntralinksAccess(oldList, null);
    }
     public static void OnAfterInsert(contact[] newList){
        
        updateCPIntralinksAccess(newList, null);
    }
    public static void OnUndelete(contact[] newList){ 
         
         updateCPIntralinksAccess(newList,null);
    }
    private static void updateSalutationOnCommunicationContent(List<Contact> newContacts, Map<id,Contact> oldContacts){
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((String.isNotBlank(con.email) && !con.email.contains('@test.com'))  && ((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
                                                                                                  (con.FirstName != oldContacts.get(con.id).FirstName)) || (con.International_Salutation_Type__c!= oldContacts.get(con.Id).International_Salutation_Type__c || String.isBlank(oldContacts.get(con.Id).email))
              )
            {
                        contactsToProcess.add(con.id);
            }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Company__c, RecordType.Name from Correspondence_Preference__c where Recipient_Type__c = 'To' AND Contact__c In :contactsToProcess]);
            set<string> cpNames = new Set<String>();
            set<string> accountNames = new Set<String>();
            for(Correspondence_Preference__c cp : corrPrefToProcess) {
                cpNames.add(cp.RecordType.Name);
                accountNames.add(cp.Company__c);
            }
            List<Communications_Content__c> ccList = new List<Communications_Content__c>([Select Id,Salutation__c,Company__c,Correspondence_Preference__c from Communications_Content__c where Company__c IN :accountNames AND Correspondence_Preference__c IN :cpNames]);
            CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,true);
            handler.updateSalutationField(null,ccList); 
        }
    private static void updateSortedCorrespondencePreferneceData(List<Contact> newContacts, Map<id,Contact> oldContacts) {
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
               (con.Email != oldContacts.get(con.id).Email)
              ) {
                  contactsToProcess.add(con.id);
              }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Commitment__c, RecordTypeId from Correspondence_Preference__c where Contact__c In :contactsToProcess]);
            
            CorrespondenceTriggerHandler.getCommRecTypeData(corrPrefToProcess,null,false);
        }
    }
    private static id fetchRecordTypeId(string sObj, string rt) {
        System.debug('fetch params>>'+sObj + '<<<rt>>>'+rt);
        String recordtypeId = Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId(); 
        return Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId();
    }
    public static void updateCPIntralinksAccess(List<Contact> newContacts, Map<id,Contact> OldMap){
        Map<ID,List<contact>> mapConWithAcc = new Map<ID,List<contact>>();
        List<account> accList = new List<account>();
        String temp= '';
        Map<Id,String> accUpdateMap = new Map<Id,String>();
        Set<ID> accid = new Set<ID>();
        for(contact con: newContacts){
            if(oldMap == null || oldMap.get(con.id).CP_Intralinks_Access__c != con.CP_Intralinks_Access__c ){ 
                accid.add(con.accountid); 
            }
        }
        for(Account acc : [Select id,CP_Intralinks_Access__c,(Select id,CP_Intralinks_Access__c,accountid from contacts) from account where id in :accid]) {
            set<string> val = new set<string>();
            String finalVal = '' ;
            for(Contact ct : acc.contacts) {
                if(String.isNOtBlank(ct.CP_Intralinks_Access__c)) {
                }
            }
            List<String> valList = new List<String>();
            valList.addAll(val);
            if(valList.size() > 0) {
                finalVal = String.join(valList,',');
            }
            acc.CP_Intralinks_Access__c = finalVal ;
            accList.add(acc);
        }
        if(accList.size() > 0) {
            update accList ;
        }
    }
}


 
Trigger 'CommunicationContentTrigger' is written on a custom object 'Communication_Content__C' to populate value from First Name field on Contact object to Salutation field on Communication Content object. But the value on Salution is not getting updated when a contact is deleted. I am new to apex. Would anyone be able to help me with modifying the trigger.

Here is the Trigger:
trigger CommunicationContentTrigger on Communications_Content__c (
  before insert, 
  before update) {
    CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,false);
    if (Trigger.isBefore) {
        //call your handler.before method
      if(Trigger.isInsert) {
        handler.OnBeforeInsert(Trigger.new);
      } else if(Trigger.isUpdate) { 
        handler.OnBeforeUpdate(Trigger.oldMap,Trigger.new);
      }
      
    } 
}

Here is apex class handler
public with sharing class CommunicationContentTriggerHandler {

  private boolean m_isExecuting = false;
  private boolean force_update = false;
  private integer BatchSize = 0;

  public CommunicationContentTriggerHandler(boolean isExecuting, integer size, boolean forceUpdate){
    m_isExecuting = isExecuting;
    BatchSize = size;
    force_update = forceUpdate ;
  }

  public void OnBeforeInsert(Communications_Content__c[] newRecords){

    updateSalutationField(null,newRecords);
   
  }


  public void OnBeforeUpdate(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] updatedRecords){
    
  }

  public void updateSalutationField(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] newRecords) {
     
      Set<Id> companyID = new Set<Id>(); 
      Set<String> correspPref = new Set<String>(); 
      
      List<Communications_Content__c> ccToUpdate = new List<Communications_Content__c>();
      for(Communications_Content__c cc :newRecords) {
          System.debug('---- Company: ' + cc.Company__c + ' Correspondence_Preference__c: ' + cc.Correspondence_Preference__c); 
          if(oldRecords == null && cc.Company__c != null && cc.Correspondence_Preference__c != null) {
             
              companyID.add(cc.Company__c); 
              correspPref.add(cc.Correspondence_Preference__c); 
          }
          
          if(oldRecords != null && (cc.Company__c != oldRecords.get(cc.Id).Company__c
             || cc.Correspondence_Preference__c  != oldRecords.get(cc.Id).Correspondence_Preference__c )) {

                          if(cc.Company__c == null || cc.Correspondence_Preference__c == null) {
                                 cc.Salutation__c  = '' ;
                                 
                                 cc.Correspondence_Preference__c='';
                                 ccToUpdate.add(cc);              
                          }
                           else {
                               System.debug('Not null');
                       
                                companyID.add(cc.Company__c);
                        correspPref.add(cc.Correspondence_Preference__c);
                       }
          }

         
          if(oldRecords != null && cc.Company__c != null && cc.Correspondence_Preference__c != null 
            && (cc.Company__c == oldRecords.get(cc.Id).Company__c
             || cc.Correspondence_Preference__c  == oldRecords.get(cc.Id).Correspondence_Preference__c )){ 
                     System.debug('Blank update');
                 
                     companyID.add(cc.Company__c); 
                   correspPref.add(cc.Correspondence_Preference__c); 
          }
        
      }
    System.debug('MKlich Companies: '+ CompanyID); 
    System.debug('MKlich Coresponding Pref: : '+ correspPref); 

     
      if(companyID.size() > 0 && correspPref.size() > 0) {
      Map<string,List<String>> accCPandContactMap = new Map<string,List<String>>();
    
       for(Correspondence_Preference__c cp : [Select Id,Contact__c,Contact__r.FirstName,Contact__r.Email,RecordType.Name,Company__c,Seniority_Rank_On_Contact__c, Contact__r.International_Salutation_Type__c, Contact__r.LastName from Correspondence_Preference__c where Company__c IN :companyID AND RecordType.Name IN :correspPref AND Recipient_Type__c = 'To' AND Contact__r.Email != null AND ( NOT Contact__r.Email LIKE '%@generalatlantic.com' ) order by Seniority_Rank_On_Contact__c, Contact__r.FirstName]){
      
      
        string key = cp.Company__c+cp.RecordType.Name.toLowerCase();
        System.debug('Key is: '+key);
        if(!accCPandContactMap.containsKey(key)) {
          accCPandContactMap.put(key,new List<String>{});
          System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.keySet());
          System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.Values());
        }
       
        String name = '';
        if(cp.Contact__r.International_Salutation_Type__c != null){
          if(cp.Contact__r.International_Salutation_Type__c == 'Japanese'){
            name = cp.Contact__r.LastName + '-san';
          }
          else if(cp.Contact__r.International_Salutation_Type__c == 'Thai' && cp.Contact__r.FirstName != null){
            name = 'Khun ' + cp.Contact__r.FirstName;
          }
        }
        else if(cp.Contact__r.FirstName != null){
          name = cp.Contact__r.FirstName;
        }
        if(name != ''){
          accCPandContactMap.get(key).add(name);
            
        }

        
      }
      for(Communications_Content__c cc :newRecords) {
        if(cc.Company__c != null && cc.Correspondence_Preference__c != null) {
          string key = cc.Company__c+cc.Correspondence_Preference__c.toLowerCase();
          string valToUpdate = '' ;
          if(accCPandContactMap.containsKey(key)) {
            List<String> contacts = accCPandContactMap.get(key);
            valToUpdate = replaceLast(String.join(contacts,', '),', ',' and ');
            
            valToUpdate += ',';
          }
          cc.Salutation__c = valToUpdate ;
          
          if(String.isEmpty(cc.Salutation__c)) {
            cc.Correspondence_Preference__c = '';
          }
          
          ccToUpdate.add(cc);
        }
      }
    }
    
    if(force_update && ccToUpdate.size() > 0 ){
        update ccToUpdate;
    }

  }

  private string replaceLast(string mainStr, string substrToReplace, string replacedBy) {
    integer index = mainStr.lastIndexOf(substrToReplace);
    if (index == -1)
      return mainStr;

    return mainStr.substring(0, index) + replacedBy + mainStr.substring(index+substrToReplace.length());
  }
}

​​​​​​​
How can I alert users that they have added duplicate team members on an account.
Logic to dedupe the team members is already there, just need to let the users know that the duplicate that they added has been removed
<apex:page standardController="Account" extensions="ManageCompanyTeamMember" lightningStylesheets="true" showQuickActionVfHeader="false">
    <apex:slds />
    
    <script>
        function showLoading(){
            document.getElementById('loading-div').style.display = "block";
        }
    </script>
    
    <apex:sectionHeader title="Team Members" subtitle="{!Account.Name}"/>
    
    <apex:form id="teammemberPanel" >
        
        <div class="slds-scope" id="loading-div" style="display:none;">
            <div id="spinner" class="slds-spinner_container slds-is-absolute" >
                <div role="status" class="slds-spinner slds-spinner--large slds-spinner--brand">
                    <div class="slds-spinner__dot-a"></div>
                    <div class="slds-spinner__dot-b"></div>
                </div>
            </div>
        </div>
        
        <apex:variable value="{!0}" var="row"/>
        
        <apex:pageBlock title="">
            <apex:pageBlockButtons >
                <div align="center">
                    <apex:commandButton value="Add" action="{!add}" reRender="teammemberPanel" immediate="true"  style="margin:3px; background-color: #1b5297; color: white"/>
                    <apex:commandButton value="Save" action="{!save}" disabled="{!teammember.empty}" onclick="showLoading()"
                                        style="margin:3px; background-color: #1b5297; color: white"/>
                    <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"  style="margin:3px; background-color: #1b5297; color: white"/>
                </div>
            </apex:pageBlockButtons>
            
            <apex:panelGrid styleClass="list" cellspacing="0" cellpadding="0" border="0" rendered="{!teammember.empty}">
                <apex:outputText value="No records to display"/>
            </apex:panelGrid>
            
            <apex:pageBlockTable value="{!teammember}" var="f" rendered="{!NOT(teammember.empty)}">
                <apex:column headerValue="Action" styleClass="actionColumn">
                    <apex:commandLink value="X" action="{!remove}" styleClass="actionLink" reRender="teammemberPanel">
                        <apex:param name="row" value="{!row}" />
                    </apex:commandLink>
                    <apex:variable value="{!row+1}" var="row"/>
                </apex:column>
                
                
                <apex:column headerValue="Team Member">
                    <apex:inputField value="{!f.Team_Member__c}" style="width: 0px;"/>
                </apex:column>
                
                
                <apex:column headerValue="Sort Order">
                    <apex:inputField value="{!f.Weekly_Update_Sort_Order__c}" style="width: 80px;"/>
                </apex:column>
                
            </apex:pageBlockTable>  
            
        </apex:pageBlock>
        <!--
    <c:Typeahead object="User" placeHolder="Search For User" searchScope="NAME" styleClass="search"  />  -->
    </apex:form>
</apex:page>
 
public class ManageCompanyTeamMember {
    public transient String dmessage { get; set; }
    public List<Company_Team_Member__c> teammember { get; private set; }
    public List<Company_Team_Member__c> deleteTeammember { get; private set; }
    private Id accountId;

    public ManageCompanyTeamMember(ApexPages.StandardController controller) {
        accountId = controller.getRecord().Id;
        refresh();
        
    }

    public void refresh() {
        teammember = new List<Company_Team_Member__c>();
        deleteTeammember = new List<Company_Team_Member__c>();
        for (Company_Team_Member__c f : [SELECT Id, Name, Company__c, Team_Member__c, Weekly_Update_Sort_Order__c FROM Company_Team_Member__c WHERE Company__c=:accountId]) {
            teammember.add(f);
        }
        for (Integer i = 0; i < 1; i++) {
            add();
        }
    }

    public void add() {
        teammember.add(new Company_Team_Member__c(
            Company__c = accountId
        ));
    }
    
    public void remove() {
        try {
            Integer row = Decimal.valueOf(ApexPages.currentPage().getParameters().get('row')).intValue();
            Company_Team_Member__c cf = teammember.remove(row);            
            if (cf.Id != null) {
                deleteTeammember.add(cf);                
            }
        } catch (Exception e) {
        }        
    }
    
    public PageReference save() {
        
        Set<Id> uniqueMemberId = new Set<Id>();
        
        PageReference ref = null;
        List<Company_Team_Member__c> teammemberList = new List<Company_Team_Member__c>();
        for (Company_Team_Member__c t : teammember) {
              if (t.Team_Member__c != null && uniqueMemberId.add( t.Team_Member__c  )) {
                teammemberList.add(t);
            }            
        } 
        if (!deleteTeammember.isEmpty()) {
            Database.delete(deleteTeammember, false);
        }   
         if (!teammemberList.isEmpty()) {           
            teammember = teammemberList;            
            upsert teammember;
            refresh();
        } 
        return new PageReference('/' + accountId);
    }
}

 
I am getting NullPointerException error while saving a new contact record.
Here is the exact error message:
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()

I am new to apex coding and this code isn't written by me, so finding it hard to troubleshoot the issue. Would anyone be able to help?
Here is Apex Trigger:
trigger ContactTrigger on Contact (before delete, after insert, after update,after delete,after undelete) { 
    if(Trigger.isAfter) {
            if(Trigger.isUpdate) {
            ContactTriggerHandler.onAfterUpdate(Trigger.new,Trigger.oldMap);
            }
            
            if(Trigger.isInsert){
             ContactTriggerHandler.OnAfterInsert(Trigger.new);
            }
            
            if(Trigger.IsDelete){
                ContactTriggerHandler.OnAfterDelete(Trigger.old);
            }
            
            if(Trigger.IsUnDelete){
                ContactTriggerHandler.OnUndelete(Trigger.new);
            }
    }
    if(Trigger.isBefore){
            if(Trigger.IsDelete){
            }
    }
}

And here is the Apex Class:
public class ContactTriggerHandler {
    
    public static void onAfterUpdate(List<Contact> newContacts, Map<id,Contact> oldContacts) {
            updateSortedCorrespondencePreferneceData(newContacts,oldContacts);
            updateSalutationOnCommunicationContent(newContacts,oldContacts); 
            id cpCapitalPartnerRT = fetchRecordTypeId('Account','CP Capital Partner');
            List<Contact> newList = new List<Contact>();
            
            set<Id> oldAndNewAccountIds = new set<id>();
            for(Contact con : newContacts) {
                oldAndNewAccountIds.add(con.accountId);
                oldAndNewAccountIds.add(oldContacts.get(con.id).accountId);
            }
            
            Map<Id,Account> accountDetails = new Map<Id,Account>([Select Id,RecordTypeId from Account where Id in :oldAndNewAccountIds]);
            for(Contact con : newContacts) {
                    if(accountDetails !=NULL && con!= Null && con.accountId != oldContacts.get(con.Id).accountId  ) { 
                        if(  accountDetails.containsKey(con.accountId) && (accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT || accountDetails.get(oldContacts.get(con.Id).accountId).recordTypeId == cpCapitalPartnerRT) ) {
                            newList.add(con);
                        }

                    } else if(accountDetails !=NULL && con!= Null && accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT ) {  
                        newList.add(con);
                    }
            }        
           
            Email_InformationClass.getEmailTableData(newList,oldContacts); 
			
            updateCPIntralinksAccess(newContacts,oldContacts);
			
    }
    
	
    public static void OnAfterDelete(contact[] oldList){
        
        updateCPIntralinksAccess(oldList, null);
    }
    
     public static void OnAfterInsert(contact[] newList){
        
        updateCPIntralinksAccess(newList, null);
    }
     
    public static void OnUndelete(contact[] newList){ 
         
         updateCPIntralinksAccess(newList,null);
    }
	
    private static void updateSalutationOnCommunicationContent(List<Contact> newContacts, Map<id,Contact> oldContacts){
        set<id> contactsToProcess = new set<id>();

        for(Contact con : newContacts) {
		
            if((String.isNotBlank(con.email) && !con.email.contains('@generalatlantic.com'))  && ((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
                                                                                                  (con.FirstName != oldContacts.get(con.id).FirstName)) || (con.International_Salutation_Type__c!= oldContacts.get(con.Id).International_Salutation_Type__c || String.isBlank(oldContacts.get(con.Id).email))
              )
			  
            {
                
                        contactsToProcess.add(con.id);
            }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Company__c, RecordType.Name from Correspondence_Preference__c where Recipient_Type__c = 'To' AND Contact__c In :contactsToProcess]);
            set<string> cpNames = new Set<String>();
            set<string> accountNames = new Set<String>();
            for(Correspondence_Preference__c cp : corrPrefToProcess) {
                cpNames.add(cp.RecordType.Name);
                accountNames.add(cp.Company__c);
            }
            List<Communications_Content__c> ccList = new List<Communications_Content__c>([Select Id,Salutation__c,Company__c,Correspondence_Preference__c from Communications_Content__c where Company__c IN :accountNames AND Correspondence_Preference__c IN :cpNames]);
            CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,true);
            handler.updateSalutationField(null,ccList); 
        }
		
    private static void updateSortedCorrespondencePreferneceData(List<Contact> newContacts, Map<id,Contact> oldContacts) {
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
               (con.Email != oldContacts.get(con.id).Email)
              ) {
                  contactsToProcess.add(con.id);
              }
        }
        
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Commitment__c, RecordTypeId from Correspondence_Preference__c where Contact__c In :contactsToProcess]);
            
            CorrespondenceTriggerHandler.getCommRecTypeData(corrPrefToProcess,null,false);
        }
    }
	
    private static id fetchRecordTypeId(string sObj, string rt) {
        System.debug('fetch params>>'+sObj + '<<<rt>>>'+rt);
        String recordtypeId = Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId(); 
        return Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId();
		
    }
	
    public static void updateCPIntralinksAccess(List<Contact> newContacts, Map<id,Contact> OldMap){
        
        Map<ID,List<contact>> mapConWithAcc = new Map<ID,List<contact>>();
        List<account> accList = new List<account>();
        String temp= '';
        Map<Id,String> accUpdateMap = new Map<Id,String>();
        Set<ID> accid = new Set<ID>();
        
        for(contact con: newContacts){
            if(oldMap == null || (oldMap.get(con.id).CP_Intralinks_Access__c != con.CP_Intralinks_Access__c)){ 
                accid.add(con.accountid); 
            }
            if(oldMap.get(con.Id).AccountId != con.AccountId)
            {
               accid.add(con.AccountId);
               accid.add(oldMap.get(con.Id).AccountId);
            }
        }
        

		
        for(Account acc : [Select id,CP_Intralinks_Access__c,(Select id,CP_Intralinks_Access__c,accountid from contacts) from account where id in :accid]) {
            set<string> val = new set<string>();
            String finalVal = '' ;
            for(Contact ct : acc.contacts) {
                if(String.isNOtBlank(ct.CP_Intralinks_Access__c)) {
                    val.addAll(ct.CP_Intralinks_Access__c.split(';')); 
                }
            }
            List<String> valList = new List<String>();
            valList.addAll(val);
            if(valList.size() > 0) {
                finalVal = String.join(valList,',');
            }
            acc.CP_Intralinks_Access__c = finalVal ;
            accList.add(acc);
        }
        if(accList.size() > 0) {
            update accList ;
        }
    }
}

 
I am receiving error email from ApexApplication <info@salesforce.com> with following content:
Subject: Developer script exception from ABC : ContactTrigger : ContactTrigger: execution of AfterUpdate  caused by: System.NullPointerException: Attempt to de-reference a null object  ()
Apex script unhandled trigger exception by user/organization: 

ContactTrigger: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.ContactTriggerHandler.onAfterUpdate: line 28, column 1
Trigger.ContactTrigger: line 15, column 1

Here is the Trigger:
trigger ContactTrigger on Contact (after insert, after update,after delete,after undelete) 
    
    if(Trigger.isAfter) {
            if(Trigger.isUpdate) {
            ContactTriggerHandler.onAfterUpdate(Trigger.new,Trigger.oldMap);
            }
            
            if(Trigger.isInsert){
             ContactTriggerHandler.OnAfterInsert(Trigger.new);
            }
            
            if(Trigger.IsDelete){
                ContactTriggerHandler.OnAfterDelete(Trigger.old);
            }
            
            if(Trigger.IsUnDelete){
                ContactTriggerHandler.OnUndelete(Trigger.new);
            }
    }
     
}

Here is apex class handler
public class ContactTriggerHandler {
    public static void onAfterUpdate(List<Contact> newContacts, Map<id,Contact> oldContacts) {
            updateSortedCorrespondencePreferneceData(newContacts,oldContacts);
            updateSalutationOnCommunicationContent(newContacts,oldContacts); 
            id cpCapitalPartnerRT = fetchRecordTypeId('Account','CP Capital Partner');
            List<Contact> newList = new List<Contact>();
            set<Id> oldAndNewAccountIds = new set<id>();
            for(Contact con : newContacts) {
                oldAndNewAccountIds.add(con.accountId);
                oldAndNewAccountIds.add(oldContacts.get(con.id).accountId);
            }
            Map<Id,Account> accountDetails = new Map<Id,Account>([Select Id,RecordTypeId from Account where Id in :oldAndNewAccountIds]);
            for(Contact con : newContacts) {
                    if(accountDetails !=NULL && con!= Null && con.accountId != oldContacts.get(con.Id).accountId  ) { 
                        if(  accountDetails.containsKey(con.accountId) && (accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT || accountDetails.get(oldContacts.get(con.Id).accountId).recordTypeId == cpCapitalPartnerRT) ) {
                            newList.add(con);
                        }
                    } else if(accountDetails !=NULL && con!= Null && accountDetails.get(con.accountId).recordTypeId == cpCapitalPartnerRT ) { 
                        newList.add(con);
                    }
            }        
            Email_InformationClass.getEmailTableData(newList,oldContacts); 
            updateCPIntralinksAccess(newContacts,oldContacts);
    }
    public static void OnAfterDelete(contact[] oldList){
        
        updateCPIntralinksAccess(oldList, null);
    }
     public static void OnAfterInsert(contact[] newList){
        
        updateCPIntralinksAccess(newList, null);
    }
    public static void OnUndelete(contact[] newList){ 
         
         updateCPIntralinksAccess(newList,null);
    }
    private static void updateSalutationOnCommunicationContent(List<Contact> newContacts, Map<id,Contact> oldContacts){
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((String.isNotBlank(con.email) && !con.email.contains('@test.com'))  && ((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
                                                                                                  (con.FirstName != oldContacts.get(con.id).FirstName)) || (con.International_Salutation_Type__c!= oldContacts.get(con.Id).International_Salutation_Type__c || String.isBlank(oldContacts.get(con.Id).email))
              )
            {
                        contactsToProcess.add(con.id);
            }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Company__c, RecordType.Name from Correspondence_Preference__c where Recipient_Type__c = 'To' AND Contact__c In :contactsToProcess]);
            set<string> cpNames = new Set<String>();
            set<string> accountNames = new Set<String>();
            for(Correspondence_Preference__c cp : corrPrefToProcess) {
                cpNames.add(cp.RecordType.Name);
                accountNames.add(cp.Company__c);
            }
            List<Communications_Content__c> ccList = new List<Communications_Content__c>([Select Id,Salutation__c,Company__c,Correspondence_Preference__c from Communications_Content__c where Company__c IN :accountNames AND Correspondence_Preference__c IN :cpNames]);
            CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,true);
            handler.updateSalutationField(null,ccList); 
        }
    private static void updateSortedCorrespondencePreferneceData(List<Contact> newContacts, Map<id,Contact> oldContacts) {
        set<id> contactsToProcess = new set<id>();
        for(Contact con : newContacts) {
            if((con.Seniority_Rank__c != oldContacts.get(con.Id).Seniority_Rank__c) ||
               (con.Email != oldContacts.get(con.id).Email)
              ) {
                  contactsToProcess.add(con.id);
              }
        }
        if(contactsToProcess.size() > 0) {
            List<Correspondence_Preference__c> corrPrefToProcess = new List<Correspondence_Preference__c>([Select Id, Commitment__c, RecordTypeId from Correspondence_Preference__c where Contact__c In :contactsToProcess]);
            
            CorrespondenceTriggerHandler.getCommRecTypeData(corrPrefToProcess,null,false);
        }
    }
    private static id fetchRecordTypeId(string sObj, string rt) {
        System.debug('fetch params>>'+sObj + '<<<rt>>>'+rt);
        String recordtypeId = Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId(); 
        return Schema.getGlobalDescribe().get(sObj).getDescribe().getRecordTypeInfosByName().get(rt).getRecordTypeId();
    }
    public static void updateCPIntralinksAccess(List<Contact> newContacts, Map<id,Contact> OldMap){
        Map<ID,List<contact>> mapConWithAcc = new Map<ID,List<contact>>();
        List<account> accList = new List<account>();
        String temp= '';
        Map<Id,String> accUpdateMap = new Map<Id,String>();
        Set<ID> accid = new Set<ID>();
        for(contact con: newContacts){
            if(oldMap == null || oldMap.get(con.id).CP_Intralinks_Access__c != con.CP_Intralinks_Access__c ){ 
                accid.add(con.accountid); 
            }
        }
        for(Account acc : [Select id,CP_Intralinks_Access__c,(Select id,CP_Intralinks_Access__c,accountid from contacts) from account where id in :accid]) {
            set<string> val = new set<string>();
            String finalVal = '' ;
            for(Contact ct : acc.contacts) {
                if(String.isNOtBlank(ct.CP_Intralinks_Access__c)) {
                }
            }
            List<String> valList = new List<String>();
            valList.addAll(val);
            if(valList.size() > 0) {
                finalVal = String.join(valList,',');
            }
            acc.CP_Intralinks_Access__c = finalVal ;
            accList.add(acc);
        }
        if(accList.size() > 0) {
            update accList ;
        }
    }
}


 
Trigger 'CommunicationContentTrigger' is written on a custom object 'Communication_Content__C' to populate value from First Name field on Contact object to Salutation field on Communication Content object. But the value on Salution is not getting updated when a contact is deleted. I am new to apex. Would anyone be able to help me with modifying the trigger.

Here is the Trigger:
trigger CommunicationContentTrigger on Communications_Content__c (
  before insert, 
  before update) {
    CommunicationContentTriggerHandler handler = new CommunicationContentTriggerHandler(true,200,false);
    if (Trigger.isBefore) {
        //call your handler.before method
      if(Trigger.isInsert) {
        handler.OnBeforeInsert(Trigger.new);
      } else if(Trigger.isUpdate) { 
        handler.OnBeforeUpdate(Trigger.oldMap,Trigger.new);
      }
      
    } 
}

Here is apex class handler
public with sharing class CommunicationContentTriggerHandler {

  private boolean m_isExecuting = false;
  private boolean force_update = false;
  private integer BatchSize = 0;

  public CommunicationContentTriggerHandler(boolean isExecuting, integer size, boolean forceUpdate){
    m_isExecuting = isExecuting;
    BatchSize = size;
    force_update = forceUpdate ;
  }

  public void OnBeforeInsert(Communications_Content__c[] newRecords){

    updateSalutationField(null,newRecords);
   
  }


  public void OnBeforeUpdate(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] updatedRecords){
    
  }

  public void updateSalutationField(Map<Id,Communications_Content__c> oldRecords, Communications_Content__c[] newRecords) {
     
      Set<Id> companyID = new Set<Id>(); 
      Set<String> correspPref = new Set<String>(); 
      
      List<Communications_Content__c> ccToUpdate = new List<Communications_Content__c>();
      for(Communications_Content__c cc :newRecords) {
          System.debug('---- Company: ' + cc.Company__c + ' Correspondence_Preference__c: ' + cc.Correspondence_Preference__c); 
          if(oldRecords == null && cc.Company__c != null && cc.Correspondence_Preference__c != null) {
             
              companyID.add(cc.Company__c); 
              correspPref.add(cc.Correspondence_Preference__c); 
          }
          
          if(oldRecords != null && (cc.Company__c != oldRecords.get(cc.Id).Company__c
             || cc.Correspondence_Preference__c  != oldRecords.get(cc.Id).Correspondence_Preference__c )) {

                          if(cc.Company__c == null || cc.Correspondence_Preference__c == null) {
                                 cc.Salutation__c  = '' ;
                                 
                                 cc.Correspondence_Preference__c='';
                                 ccToUpdate.add(cc);              
                          }
                           else {
                               System.debug('Not null');
                       
                                companyID.add(cc.Company__c);
                        correspPref.add(cc.Correspondence_Preference__c);
                       }
          }

         
          if(oldRecords != null && cc.Company__c != null && cc.Correspondence_Preference__c != null 
            && (cc.Company__c == oldRecords.get(cc.Id).Company__c
             || cc.Correspondence_Preference__c  == oldRecords.get(cc.Id).Correspondence_Preference__c )){ 
                     System.debug('Blank update');
                 
                     companyID.add(cc.Company__c); 
                   correspPref.add(cc.Correspondence_Preference__c); 
          }
        
      }
    System.debug('MKlich Companies: '+ CompanyID); 
    System.debug('MKlich Coresponding Pref: : '+ correspPref); 

     
      if(companyID.size() > 0 && correspPref.size() > 0) {
      Map<string,List<String>> accCPandContactMap = new Map<string,List<String>>();
    
       for(Correspondence_Preference__c cp : [Select Id,Contact__c,Contact__r.FirstName,Contact__r.Email,RecordType.Name,Company__c,Seniority_Rank_On_Contact__c, Contact__r.International_Salutation_Type__c, Contact__r.LastName from Correspondence_Preference__c where Company__c IN :companyID AND RecordType.Name IN :correspPref AND Recipient_Type__c = 'To' AND Contact__r.Email != null AND ( NOT Contact__r.Email LIKE '%@generalatlantic.com' ) order by Seniority_Rank_On_Contact__c, Contact__r.FirstName]){
      
      
        string key = cp.Company__c+cp.RecordType.Name.toLowerCase();
        System.debug('Key is: '+key);
        if(!accCPandContactMap.containsKey(key)) {
          accCPandContactMap.put(key,new List<String>{});
          System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.keySet());
          System.debug('Inside the if accCPandContactMap: '+accCPandContactMap.Values());
        }
       
        String name = '';
        if(cp.Contact__r.International_Salutation_Type__c != null){
          if(cp.Contact__r.International_Salutation_Type__c == 'Japanese'){
            name = cp.Contact__r.LastName + '-san';
          }
          else if(cp.Contact__r.International_Salutation_Type__c == 'Thai' && cp.Contact__r.FirstName != null){
            name = 'Khun ' + cp.Contact__r.FirstName;
          }
        }
        else if(cp.Contact__r.FirstName != null){
          name = cp.Contact__r.FirstName;
        }
        if(name != ''){
          accCPandContactMap.get(key).add(name);
            
        }

        
      }
      for(Communications_Content__c cc :newRecords) {
        if(cc.Company__c != null && cc.Correspondence_Preference__c != null) {
          string key = cc.Company__c+cc.Correspondence_Preference__c.toLowerCase();
          string valToUpdate = '' ;
          if(accCPandContactMap.containsKey(key)) {
            List<String> contacts = accCPandContactMap.get(key);
            valToUpdate = replaceLast(String.join(contacts,', '),', ',' and ');
            
            valToUpdate += ',';
          }
          cc.Salutation__c = valToUpdate ;
          
          if(String.isEmpty(cc.Salutation__c)) {
            cc.Correspondence_Preference__c = '';
          }
          
          ccToUpdate.add(cc);
        }
      }
    }
    
    if(force_update && ccToUpdate.size() > 0 ){
        update ccToUpdate;
    }

  }

  private string replaceLast(string mainStr, string substrToReplace, string replacedBy) {
    integer index = mainStr.lastIndexOf(substrToReplace);
    if (index == -1)
      return mainStr;

    return mainStr.substring(0, index) + replacedBy + mainStr.substring(index+substrToReplace.length());
  }
}

​​​​​​​