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
MarellaMarella 

No of related records per Parent Object

Hi All,

 

I have written two triggers to update the "No of Contacts" for each account.  Both the triggers are working fine. I have implemented the first trigger from the below one and is currently in use. Once i had the trigger working fine, i tried to update the list of accounts with no of contacts by using the data loader. But not able to update multiple accounts with no of contacts field. Please have a look on below triggers:

---------------------------------------------------------------------------------------------------------------------------------------------

trigger tgrContactCount on Contact (after delete, after insert, after undelete, after update) {
    
    Contact [] con;
    if(Trigger.isDelete || Trigger.isUpdate)
    {
        con = trigger.old;
    }
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete)
    {
        con = trigger.new;
    }
    String cid = null;
    cid = con[0].AccountId;        
   
    Integer i = [select count() from Contact where AccountId = :cid];    

        // update the account record
    Account [] a =[select id, No_of_Contacts__c from Account where id = :cid];
 
    a[0].No_of_Contacts__c = i;    
        // update database
        update a[0];    

} 

------------------------------------------------------------ 2nd one as below:-------------------------------------------------------

trigger tgrContactCount on Contact (after delete, after insert, after undelete, after update) {
   
    public set<Id> AccIDs = new Set<Id>();
       
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUnDelete)
    {
        for(Contact con: Trigger.new)
        {
            AccIds.add(con.AccountId);               
        }
    }
    if(Trigger.isDelete || Trigger.isUpdate)
    {
        for(Contact con: Trigger.old)
        {
            AccIds.add(con.AccountId);
        }
    }
   
    Map<Id, Contact>ConsWithAccs = new Map<ID, Contact>([select Id, AccountId from Contact where AccountId in :AccIds]);
   
    Map<ID, Account>accstoUpdate = new Map<ID, Account>([select Id, No_of_Contacts__c from Account where Id in : AccIds]);
   
    for(Account a :accstoUpdate.values())
    {
        Set<Id>conIds = new Set<Id>();
        for(Contact c : ConsWithAccs.values())
        {
            if(c.AccountId==a.Id)
            conIds.add(c.Id);
        }
       
        if(a.No_of_Contacts__c!=conIds.size())
        a.No_of_Contacts__c=conIds.size();   
    }   
   
    update accstoUpdate.values();
}

------------------------------------------------------------------------------------------------------------------------------

 

Both the triggers are working fine for individual records. But when i tried to mass update, its not working and facing some Limit issues as well. Please do suggest me to use any of the above trigger to handle the bulk updates as well.

 

Hope i am clear in summarising my issue.

 

Thanks,

Veeru

 

pankaj.raijadepankaj.raijade

The first trigger is not bulkified list second one. so it will update only first record.

 

for second trigger  you are using for loop inside for loop so for bulk records it will hit governer limit.

 

instaind you can create map<Id, list<CONTACT>>  

loop through contact list put account is as key and add contact in contact list.

 

then loop through account get list of contact from above map and get size and update.

 

 

MarellaMarella

Hi Pankaj,

 

Please can you add your logic in the trigger and send me, so that i can have a try as i am new to triggers and apex coding.

 

Thanks,

Veeru

pankaj.raijadepankaj.raijade

create map<Id, list<CONTACT>>  

 

loop contact list.

{

if map contactkey (contact.accountid)

map.get(contact.accountid).add(contact)

else

map.put(contact.accountid,new list <contact>{contact}) 

}

 

loop account list

{

if map.contains key (account.id)

account.yourfield = map.get(account.id).size()

}