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
bhanu_prakashbhanu_prakash 

trigger handler usage for trigger

Hi team,

I have designed a trigger to update date__c field of contact on account object Date__c . Now i want to modify code with help of trigger handler class . How can I acheive it ?
trigger LatestDateContact on Contact (after insert,after update,after delete) {    
    List<Account> updAcc = new List<Account>();  
    Map<id,account> mapacc = new Map<id,account>( [select id,name,Date__c,(select id,Date__c from contacts) from account]);
    if(Trigger.isDelete) {
        List<Contact> oldContatcs = Trigger.Old;
        for(Contact con: oldContatcs) {
            Id accID = con.AccountID;
            date latestdate = Date.newInstance(2008, 1, 1);
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(latestdate < c.Date__c ) {
                    latestdate = c.Date__c;
                }
            }
            acc.Date__c = latestdate;
            updAcc.add(acc);
        }
        update updAcc;
    }
    if(Trigger.isInsert || Trigger.isUpdate) {
        List<Contact> newContacts = Trigger.New;
        for(Contact con: newContacts) {
            Id accID = con.AccountID;
            date latestdate = con.Date__c;
            if(latestdate == null) {
                latestdate = Date.newInstance(2008, 1, 1);
            }
            Account acc = mapacc.get(accID);
            for(Contact c: acc.Contacts) {
                if(latestdate < c.Date__c ) {
                    latestdate = c.Date__c;
                }
            }
            acc.Date__c = latestdate;
            updAcc.add(acc);
        }    
        update updAcc;
    }    
}


Thanks for advance 
 
Best Answer chosen by bhanu_prakash
KapilCKapilC
Hi Bhanu,

Please find the updated code below.
trigger LatestDateContact on Contact (after insert,after update,after delete) {
 if(Trigger.isAfter){
		if(Trigger.isUpdate) {   
	         ContactTriggerHandler.onAfterUpdate(trigger.new,trigger.oldMap);      
	    }else if(Trigger.isInsert) {
	         ContactTriggerHandler.onAfterInsert(Trigger.new);
	    }else if(Trigger.isDelete) {
	         ContactTriggerHandler.onAfterDelete(trigger.OldMap);
	    }
	}
}
Class code is below.
public without sharing class ContactTriggerHandler {
		public static void onAfterUpdate(list<Contact> newList,Map<Id,Contact> oldMap){
			list<account> filerAccountList = filerAccounts(newList);
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
		
		public static void onAfterInsert(list<Contact> newList){
			list<account> filerAccountList = filerAccounts(newList);
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
	
		public static void onAfterDelete(Map<Id,Contact> oldMap){
			list<account> filerAccountList = filerAccounts(oldMap.values());
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
		
		private static list<account> filerAccounts(list<contact> contactList){
		 set<id> accountIdSet = new set<id>();
			for(contact con : newList){
				if(con.accountId != null){
					accountIdSet,add(con.accountId);
				}
			}
			list<account> accountToUpdateList = new list<account>();
			date latestdate = Date.newInstance(2008, 1, 1);
			for(account acc : [select Id, Date__c,(select Id,Date__c from contacts order by Date__c desc limit 1) from account]){
					for(contact con : acc.contacts){
						if(latestdate < con.Date__c){
							acc.Date__c = con.Date__c;
							accountToUpdateList.add(acc);
						}
					}
			}
			return accountToUpdateList;
		}
}



If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)

All Answers

KapilCKapilC
Hi Bhanu,

Please find the updated code below.
trigger LatestDateContact on Contact (after insert,after update,after delete) {
 if(Trigger.isAfter){
		if(Trigger.isUpdate) {   
	         ContactTriggerHandler.onAfterUpdate(trigger.new,trigger.oldMap);      
	    }else if(Trigger.isInsert) {
	         ContactTriggerHandler.onAfterInsert(Trigger.new);
	    }else if(Trigger.isDelete) {
	         ContactTriggerHandler.onAfterDelete(trigger.OldMap);
	    }
	}
}
Class code is below.
public without sharing class ContactTriggerHandler {
		public static void onAfterUpdate(list<Contact> newList,Map<Id,Contact> oldMap){
			list<account> filerAccountList = filerAccounts(newList);
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
		
		public static void onAfterInsert(list<Contact> newList){
			list<account> filerAccountList = filerAccounts(newList);
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
	
		public static void onAfterDelete(Map<Id,Contact> oldMap){
			list<account> filerAccountList = filerAccounts(oldMap.values());
			if(!filerAccountList.isEmpty()){
				update filerAccountList;
			}
		}
		
		private static list<account> filerAccounts(list<contact> contactList){
		 set<id> accountIdSet = new set<id>();
			for(contact con : newList){
				if(con.accountId != null){
					accountIdSet,add(con.accountId);
				}
			}
			list<account> accountToUpdateList = new list<account>();
			date latestdate = Date.newInstance(2008, 1, 1);
			for(account acc : [select Id, Date__c,(select Id,Date__c from contacts order by Date__c desc limit 1) from account]){
					for(contact con : acc.contacts){
						if(latestdate < con.Date__c){
							acc.Date__c = con.Date__c;
							accountToUpdateList.add(acc);
						}
					}
			}
			return accountToUpdateList;
		}
}



If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Regards,
Kapil
(forcecube@gmail.com)
This was selected as the best answer
bhanu_prakashbhanu_prakash
Thanks Kapil, You reslove my issue :) :)