function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shruti TandonShruti Tandon 

how to improve this trigger to work for delete undelete insert update on account page based on contact field value?

trigger AccountEmailContact on Contact(after delete ,after insert,after undelete,after update){
    String Em;
    Map<Id,List<Contact>> contactList=new Map<Id,List<Contact>>();
    List<Contact> con = new List<Contact>();
    List<Account> accList=new List<Account>();
    if(Trigger.isInsert || Trigger.isUnDelete){
        System.debug('Inside Here!');
        con = Trigger.new;
        System.debug('con is: ' + con);
        for(Contact c : con){
            if(contactList.containsKey(c.AccountId)){
                List<Contact> clist = contactList.get(c.AccountId);
                clist.add(c);
                contactList.put(c.AccountId,clist);
            }
            else{
                contactList.put(c.AccountId,new List<Contact>{c});
            }
        }
        List<Account> listacc = [Select EmailId__c from Account Where Id in: contactList.keySet() ];
        Map<Id,Account> mp = new Map<Id,Account>();
        for(Account ac : listacc){
            mp.put(ac.Id,ac);
        }
        List<Account> Accup = new List<Account>();        
        for(Id i : mp.keySet()){
            Account a = mp.get(i);
            List<Contact> con = contactList.get(i);
            for(Contact c : con){
                if(a.EmailId__c == null){
                    a.EmailId__c=c.Email+ ',';
                }
                else{
                   a.EmailId__c=c.Email+ ',';
                    
                }
            }
            a.EmailId__c = a.EmailId__c.substring(0,a.EmailId__c.length() - 1);
            Accup.add(a);
        }
        System.debug('Accup is: ' + Accup);
        Update Accup;
    }
    else{
        if(Trigger.isDelete){
            System.debug('It is Delete!');
            con = Trigger.old;
            for(Contact c : con){
                if(contactList.containsKey(c.AccountId)){
                    List<Contact> clist = contactList.get(c.AccountId);
                    clist.add(c);
                    contactList.put(c.AccountId,clist);
                }
                else{
                    contactList.put(c.AccountId,new List<Contact>{c});
                }
            }
            List<Account> listacc = [Select EmailId__c from Account Where Id in: contactList.keySet() ];
            Map<Id,Account> mp = new Map<Id,Account>();
            for(Account ac : listacc){
                mp.put(ac.Id,ac);
            }
            List<Account> Accup = new List<Account>();        
            for(Id i : mp.keySet()){
                Account a = mp.get(i);
                List<Contact> con = contactList.get(i);
                for(Contact c : con){
                    a.EmailId__c = a.EmailId__c.replace(c.Email,'');
                }
                a.EmailId__c = a.EmailId__c.replaceAll(',,',',');
/*                if(a.EmailId__c.charAt(a.EmailId__c.length() - 1) == (',').charAt(0)){
                    a.EmailId__c = a.EmailId__c.substring(0,a.EmailId__c.length() - 1);
                } */
                Accup.add(a);
            }
            Update Accup;        
            
        }
        else{
            if(Trigger.isUpdate && Trigger.isAfter){
                System.debug('It is Update!');
                Map<Id,Id> map1 = new Map<Id,Id>();
                Map<Id,Id> map2 = new Map<Id,Id>();
                Map<Id,Account> map3 = new Map<Id,Account>();
                Map<Id,Account> map4 = new Map<Id,Account>();
                Map<Id,Contact> temp = Trigger.oldMap;
                Map<Id,Contact> temp2 = Trigger.newMap;
                System.debug('temp is: ' + temp);
                System.debug('temp2 is: ' + temp2);
                Set<Id> st = new Set<Id>();
                for(Id i : temp.keySet()){
                    map1.put(i,temp.get(i).AccountId);
                    st.add(temp.get(i).AccountId);
                }
                List<Account> acc = [Select EmailId__c,Id from Account Where Id in: st];
                for(Account a : acc){
                    map3.put(a.Id,a);
                }
                st.clear();
                for(Id i : temp2.keySet()){
                    map2.put(i,temp2.get(i).AccountId);
                    st.add(temp2.get(i).AccountId);
                }
                acc = [Select Id,EmailId__c from Account Where Id in: st];
                for(Account a : acc){
                    map4.put(a.Id,a);
                }
                st.clear();
                Set<Account> upacc = new Set<Account>();
                for(Id i : map1.keySet()){
                    if(map1.get(i) != map2.get(i)){
                        Account a1 = map3.get(map1.get(i));
                        Account a2 = map4.get(map2.get(i));
                        a1.EmailId__c = a1.EmailId__c.replace(temp.get(i).Email,'');
                        a2.EmailId__c = a2.EmailId__c == null ? temp2.get(i).Email + ',' : a2.EmailId__c + temp2.get(i).Email + ',';
                        upacc.add(a1);
                        upacc.add(a2);
                    }
                    else{
                        if(temp.get(i).Email != temp2.get(i).Email){
                            System.debug('Inside Email Change!');
                            Account a1 = map3.get(map1.get(i));
                            a1.EmailId__c += temp2.get(i).Email + ',';
                            System.debug('a1.EmailId__c is: ' + a1.EmailId__c);
                            a1.EmailId__c = a1.EmailId__c.replaceAll(temp.get(i).Email,'');
                            System.debug('a1.EmailId__c is: ' + a1.EmailId__c);
                            upacc.add(a1);
                        }
                    }
                }
                System.debug('upacc is: ' + upacc);
                List<Account> ac = new List<Account>();
                Iterator<Account> it = upacc.iterator();
                while(it.hasNext()){
                    ac.add(it.next());
                }
                Update ac;
            }
        }
    }
}
Gregory MassieGregory Massie
This is something that I did not know properly earlier. Adding the user in the debug is simple so I can check https://www.ukbestessay.net/assignment-writing to manage the task. If there is an error, there will be a suspicious file in the computer.