• Shruti Tandon
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 10
    Replies
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;
            }
        }
    }
}

trigger AccEmail on Contact (after insert,after update) {
    String Em='';
    if(trigger.isInsert || trigger.isUpdate ){
    Set <String> accID = New Set <String> ();
    For (Contact con: Trigger.new) {
        if (con.AccountId != Null ) {
        accID.add (con.AccountId);
        }
    }
   
    if (accID.size ()> 0) {
        List <Account> upAccList = new List <Account> ();
       
      for(Account acc : [Select id, EmailId__c,
                             (Select Id, Email From Contacts)
                        From Account Where Id In : accID])
  
       for(Contact con : acc.contacts)
       {
        if(con.Email != null)
        {
            Em = Em + ', ' + con.Email;
        }
        acc.Contact_Names__c = names;
       
     upAccList.add(acc);
   }   
   update upAccList;
}
    }

trigger AccEmail on Contact (after insert,after update) {
    String Em='';
    if(trigger.isInsert || trigger.isUpdate ){
    Set <String> accID = New Set <String> ();
    For (Contact con: Trigger.new) {
        if (con.AccountId != Null ) {
        accID.add (con.AccountId);
        }
    }
   
    if (accID.size ()> 0) {
        List <Account> upAccList = new List <Account> ();
       
      for(Account acc : [Select id, EmailId__c,
                             (Select Id, Email From Contacts)
                        From Account Where Id In : accID])
  
       for(Contact con : acc.contacts)
       {
        if(con.Email != null)
        {
            Em = Em + ', ' + con.Email;
        }
        acc.Contact_Names__c = names;
       
     upAccList.add(acc);
   }   
   update upAccList;
}
    }