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
Avinaash BAvinaash B 

Trigger for related objects

Avinaash B

Hi , I have a requirement. I have a numeric field in Account called 'Total_Sum_of_Contacts__c' (parent field) and an other numeric field in Contact called 'Num__c' (child field). The numeric field(Total_Sum_of_Contacts__c) in Account should show the total of all the contacts' numeric field(Num__c). I need to write a trigger. Please someone suggest me a good trigger.
 
Best Answer chosen by Avinaash B
EldonEldon
Hey Avinash,
 
trigger testTrigger1 on Contact (after insert,after delete) {
    set<id> accId=new set<id>();
    list<contact>contactlist=new list<contact>();
    list<contact>listcon=new list<contact>();
    list<account>acclist=new list<account>();
    list<account>ToUpdateAcc=new list<account>();
    map<id,integer>mapCount=new map<id,integer>();
    
    
    if(trigger.isinsert)                              //if you are inserting contact
    {
        for(contact con:trigger.new)
        {
            accid.add(con.accountid);
            
        }
        system.debug('accid'+accid);
        
        acclist=[SELECT id,name,Total_Sum_of_Contacts__cFROM account WHERE id in :accId];
        contactlist=[SELECT id,name,accountid,Num__cFROM contact WHERE accountid in :accId];
        system.debug('acclist'+acclist);
        for(account acc:acclist){
            listcon.clear();
            for(contact c:trigger.new){
                if(c.accountid==acc.id){
                    acc.Total_Sum_of_Contacts__c=acc.Total_Sum_of_Contacts__c+c.Num__c;
                    
                }
                
            }
        }
    }
    
    if (trigger.isdelete)                          //if you are deleting  contact
    {
        for(contact con:trigger.old)
        {
            accId.add(con.accountid);
            
        }
        acclist=[SELECT id,name,Total_Sum_of_Contacts__cFROM account WHERE id in :accId];
        contactlist=[SELECT id,name,accountid,Num__cFROM contact WHERE accountid in :accId];
        system.debug('acclist'+acclist);
        for(account acc:acclist){
            listcon.clear();
            for(contact c:trigger.old){
                if(c.accountid==acc.id){
                    acc.Total_Sum_of_Contacts__c=acc.Total_Sum_of_Contacts__c-c.Num__c;
                }
                
            }
        }
        system.debug('acclistDELETE'+acclist);
    }
    
    update acclist;
    
}



Thankyou
 

All Answers

EldonEldon
trigger noofcontacts on Contact (after insert,after delete) {


    set<id>accId=new set<id>();
    
    list<contact>contactlist=new list<contact>();
    list<contact>listcon=new list<contact>();
    list<account>acclist=new list<account>();
    list<account>ToUpdateAcc=new list<account>();
    map<id,integer>mapCount=new map<id,integer>();
    
    
    if(trigger.isinsert)                              //if you are inserting contact
    {
    for(contact con:trigger.new)
    {
        accid.add(con.accountid);
    }
     }
 
 if (trigger.isdelete)                          //if you are deleting  contact
{
    for(contact con:trigger.old)
    {
        accId.add(con.accountid);
    }
}

acclist=[SELECT id,name FROM account WHERE id accId];
contactlist=[SELECT id,name,accountid FROM contact WHERE accountid accId];

for(account acc:acclist){
    listcon.clear();
    for(contact c:contactlist){
        if(c.accountid==acc.id){
            listcon.add(c);
            
            mapCount.put(c.accountid,listcon.size());
        }
    }
}

if(acclist.size()>0){
    
    for(Account a:acclist)
    {
        if(mapCount.get(a.id)==null)
            a.No_of_Contact__c =0;
        
        else
            
            a.No_of_Contact__c =mapCount.get(a.id);
        ToUpdateAcc.add(a);
    }
}


if(ToUpdateAcc.size()>0)
    
    update ToUpdateAcc;
}

Pls Mark it as best answer if it solves your problem

Thank you
Avinaash BAvinaash B
Hi, its not the size I require. I need the total sum of all contacts to be shown in Account. For ex., if Contact A enters 10, Contact B - 20, Contact C - 20, then in Account, it should be shown as 60.
EldonEldon
Hey Avinash,
 
trigger testTrigger1 on Contact (after insert,after delete) {
    set<id> accId=new set<id>();
    list<contact>contactlist=new list<contact>();
    list<contact>listcon=new list<contact>();
    list<account>acclist=new list<account>();
    list<account>ToUpdateAcc=new list<account>();
    map<id,integer>mapCount=new map<id,integer>();
    
    
    if(trigger.isinsert)                              //if you are inserting contact
    {
        for(contact con:trigger.new)
        {
            accid.add(con.accountid);
            
        }
        system.debug('accid'+accid);
        
        acclist=[SELECT id,name,Total_Sum_of_Contacts__cFROM account WHERE id in :accId];
        contactlist=[SELECT id,name,accountid,Num__cFROM contact WHERE accountid in :accId];
        system.debug('acclist'+acclist);
        for(account acc:acclist){
            listcon.clear();
            for(contact c:trigger.new){
                if(c.accountid==acc.id){
                    acc.Total_Sum_of_Contacts__c=acc.Total_Sum_of_Contacts__c+c.Num__c;
                    
                }
                
            }
        }
    }
    
    if (trigger.isdelete)                          //if you are deleting  contact
    {
        for(contact con:trigger.old)
        {
            accId.add(con.accountid);
            
        }
        acclist=[SELECT id,name,Total_Sum_of_Contacts__cFROM account WHERE id in :accId];
        contactlist=[SELECT id,name,accountid,Num__cFROM contact WHERE accountid in :accId];
        system.debug('acclist'+acclist);
        for(account acc:acclist){
            listcon.clear();
            for(contact c:trigger.old){
                if(c.accountid==acc.id){
                    acc.Total_Sum_of_Contacts__c=acc.Total_Sum_of_Contacts__c-c.Num__c;
                }
                
            }
        }
        system.debug('acclistDELETE'+acclist);
    }
    
    update acclist;
    
}



Thankyou
 
This was selected as the best answer
Avinaash BAvinaash B
Hey Eldon, You are great ! It's the perfect answer !
Thank You !