You need to sign in to do that
Don't have an account?
![shobana shobana 1 shobana shobana 1](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
How to perform trigge update operation on exiting records
I wrote a trigger which will count the contact and update in Accout field(Number_of_Contacts__c). But trigger will only work when i create a record in contact.But i want to do updation on existing records too. Can anyone help me out to solve this issue.
trigger ContactCount on Contact (after insert, after update, after delete, after undelete) { Map<Id, List<Contact>> mapAcctIdContactList = new Map<Id, List<Contact>>(); Map<Id, List<Contact>> mapAcctIdDelContactList = new Map<Id, List<Contact>>(); Set<Id> AcctIds = new Set<Id>(); List<Account> listAcct = new List<Account>(); if(trigger.isInsert) { for(Contact Con : trigger.New) { if(String.isNotBlank(Con.AccountId)) { if(!mapAcctIdContactList.containsKey(Con.AccountId)) { mapAcctIdContactList.put(Con.AccountId, new List<Contact>()); } mapAcctIdContactList.get(Con.AccountId).add(Con); AcctIds.add(Con.AccountId); } } } if(trigger.isUpdate) { for(Contact Con : trigger.New) { if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) { if(!mapAcctIdContactList.containsKey(Con.AccountId)){ mapAcctIdContactList.put(Con.AccountId, new List<Contact>()); } mapAcctIdContactList.get(Con.AccountId).add(Con); AcctIds.add(Con.AccountId); } else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) { if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){ mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>()); } mapAcctIdDelContactList.get(Con.AccountId).add(Con); AcctIds.add(trigger.oldMap.get(Con.Id).AccountId); } } } if(trigger.isUndelete) { for(Contact Con : trigger.new) { if(String.isNotBlank(Con.AccountId)){ if(!mapAcctIdContactList.containsKey(Con.AccountId)){ mapAcctIdContactList.put(Con.AccountId, new List<Contact>()); } mapAcctIdContactList.get(Con.AccountId).add(Con); AcctIds.add(Con.AccountId); } } } if(trigger.isDelete) { for(Contact Con : trigger.Old) { if(String.isNotBlank(Con.AccountId)){ if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){ mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>()); } mapAcctIdDelContactList.get(Con.AccountId).add(Con); AcctIds.add(Con.AccountId); } } } if(AcctIds.size() > 0) { listAcct = [SELECT Id, Number_of_Contacts__c FROM Account WHERE Id IN : AcctIds]; for(Account acct : listAcct) { Integer noOfConts = 0; if(mapAcctIdContactList.containsKey(acct.Id)) { noOfConts += mapAcctIdContactList.get(acct.Id).size(); } if(mapAcctIdDelContactList.containsKey(acct.Id)) { noOfConts -= mapAcctIdDelContactList.get(acct.Id).size(); } acct.Number_of_Contacts__c = acct.Number_of_Contacts__c == null ? noOfConts : (acct.Number_of_Contacts__c + noOfConts); } update listAcct; } }
There is only one option that is, you have to modify the code to execute it in the Batch Apex.
Dummy update will not work because, here if you see the code
This will not execute as we will not update the Account as part of dummy update.
Please check the below code which will do perform your requirement:
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
There is only one option that is, you have to modify the code to execute it in the Batch Apex.
Dummy update will not work because, here if you see the code
This will not execute as we will not update the Account as part of dummy update.
Please check the below code which will do perform your requirement:
Please do let me know if it helps you.
Regards,
Mahesh
Thank you for your reply. In account its not updating the value.
I am not sure how you are testing the code.
Go to Developer Console and execute the below 2 lines:
I tested the above code and it is working in my DE environment.
Please do let me know if it helps you.
Regards,
Mahesh
Thank you for your reply. I did mistake now its working fine.