Invoke a trigger to ensure that the no_of_contacts field is incremented whenever there is a new contact registered to an Account. This trigger has to work after update using Map.Can some one help me?
Map<ID,Account> mapToAccount=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts )from Account where id in:setAccId ]); Map<ID,Account> mapToAccount1=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts)from Account where id in:oldAccId ]);
<pre> trigger CountNoOfContact on Contact (after insert,after delete,after undelete) {
Set<Id> accountIds = new Set<Id> (); Map<Id,Account> accountMap = new Map<Id, Account> (); if(Trigger.isInsert || Trigger.isUndelete) {
for(Contact c : Trigger.new) {
accountIds.add(c.AccountId);
}
} if(Trigger.isDelete) {
for(Contact c : Trigger.old) {
accountIds.add(c.AccountId);
}
} accountMap = new Map <Id,Account> ([select Id, No_of_Contacts__c from Account where Id IN: accountIds]); if(Trigger.isInsert || Trigger.isUndelete) {
Please note that I have not tested this code, and there are definitely better way of writing the same code - for example, running a query on Contacts and count them and put the number in Account (using AggregateResult).
Can you please share what you have done? A code snippet would be useful.
It is easy for me to give you the code snippet, but its not the aim, the aim is to see how much effort you have put and how much you have understood.
Is that good?
Set<ID> setAccId=new Set<ID>();
Set<ID> oldAccId = new Set<ID>();
Map<Id,Integer> newContactMapping=new Map<Id,Integer>();
// List<Contact> Con=((trigger.IsInsert)||(trigger.IsUndelete))?trigger.new:trigger.old;
if((trigger.IsInsert)||(trigger.IsUndelete))
{
for(Contact con1:trigger.new)
{
if(con1.AccountId!=null)
{
setAccId.add(con1.AccountId);
if(newContactMapping.containsKey(con1.AccountId)){
newContactMapping.put(con1.AccountId, newContactMapping.get(con1.AccountId)+1);
}
else
{
newContactMapping.put(con1.AccountId, 0);
}
}
}
}
else if((trigger.IsDelete)){
for(Contact con1:trigger.old)
{
if(con1.AccountId!=null)
{
setAccId.add(con1.AccountId);
if(newContactMapping.containsKey(con1.AccountId)){
newContactMapping.put(con1.AccountId, newContactMapping.get(con1.AccountId)+1);
}
else
{
newContactMapping.put(con1.AccountId, 0);
}
}
}
}
else if(trigger.IsUpdate)
{
for(Contact con1:trigger.new)
{
if(con1.AccountId!=trigger.oldMap.get(con1.id).AccountId)
{
setAccId.add(con1.AccountId);
oldAccId.add(trigger.oldMap.get(con1.id).AccountId);
if(newContactMapping.containsKey(con1.AccountId)){
newContactMapping.put(con1.AccountId, newContactMapping.get(con1.AccountId)+1);
}
else
{
newContactMapping.put(con1.AccountId, 0);
}
}
}
}
Map<ID,Account> mapToAccount=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts )from Account where id in:setAccId ]);
Map<ID,Account> mapToAccount1=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts)from Account where id in:oldAccId ]);
for(ID accId:mapToAccount.keySet())
{
Account acc=mapToAccount.get(accId);
if(trigger.IsInsert){
acc.No_Of_Contacts__c=acc.Contacts.size()+ newContactMapping.get(accId);
}
else{
acc.No_Of_Contacts__c=acc.Contacts.size()- newContactMapping.get(accId);
}
}
update mapToAccount.values();
for(ID accId:mapToAccount1.keySet())
{
Account acc=mapToAccount1.get(accId);
if(trigger.IsInsert){
acc.No_Of_Contacts__c=acc.Contacts.size()+ newContactMapping.get(accId);
}
else{
acc.No_Of_Contacts__c=acc.Contacts.size()- newContactMapping.get(accId);
}
}
update mapToAccount1.values();
}
insert and delete is working but after update is not working.
trigger CountNoOfContact on Contact (after insert,after delete,after undelete) {
Map<Id,Account> accountMap = new Map<Id, Account> ();
if(Trigger.isInsert || Trigger.isUndelete) {
if(Trigger.isDelete) {
accountMap = new Map <Id,Account> ([select Id, No_of_Contacts__c from Account where Id IN: accountIds]);
if(Trigger.isInsert || Trigger.isUndelete) {
if(Trigger.isDelete) {
update accountMap;
</pre>
Please note that I have not tested this code, and there are definitely better way of writing the same code - for example, running a query on Contacts and count them and put the number in Account (using AggregateResult).
Let me know if this helps..
trigger countNoOfContact on Contact (after insert,after update,after delete,after undelete) {
Set<ID> setAccId=new Set<ID>();
if((trigger.IsInsert)||(trigger.IsUndelete)||(trigger.IsUpdate))
{
for(Contact con1:trigger.new)
{
if(con1.AccountId!=null)
{
setAccId.add(con1.AccountId);
}
}
}
if((trigger.IsDelete)||(trigger.IsUpdate))
{
for(Contact con1:trigger.old)
{
if(con1.AccountId!=null)
{
setAccId.add(con1.AccountId);
}
}
}
Map<ID,Account> mapToAccount=new Map<ID,Account>([select id,No_Of_Contacts__c,(select id from Contacts) from Account where id in:setAccId ]);
for(ID accId:mapToAccount.keySet())
{
Account acc=mapToAccount.get(accId);
acc.No_Of_Contacts__c=acc.Contacts.size();
}
update mapToAccount.values();
}