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
santoshk.behera1.3878016627537622E12santoshk.behera1.3878016627537622E12 

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?

goabhigogoabhigo
Hi,

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?
santoshk.behera1.3878016627537622E12santoshk.behera1.3878016627537622E12
trigger  countNoOfContact on Contact (before insert,after insert,after update,after delete,after undelete) {
   
   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.
santoshk.behera1.3878016627537622E12santoshk.behera1.3878016627537622E12
please give some suggestion.
goabhigogoabhigo
<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) {
for(Contact c : Trigger.new) {
accountMap.get(c.AccountId).No_of_Contacts__c += 1;
}
}
if(Trigger.isDelete) {
for(Contact c : Trigger.old) {
accountMap.get(c.AccountId).No_of_Contacts__c -= 1
}
}

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..
goabhigogoabhigo
Does this work? Do you need any help?
santoshk.behera1.3878016627537622E12santoshk.behera1.3878016627537622E12
no this is not working.But  this code under below is working fine.


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();
}