You need to sign in to do that
Don't have an account?
bhanu_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 ?
Thanks for advance
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
Please find the updated code below. Class code is below.
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
Please find the updated code below. Class code is below.
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Regards,
Kapil
(forcecube@gmail.com)