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
naveen reddy 68naveen reddy 68 

Question triggers

I have 2 objects product(parent object) and price(child object) and the  relationship is lookup .
I have a field amount in child object and i want  the sum of  amont field  like Roll-up summery field.
How can i get this.
Lokesh KumarLokesh Kumar
Write Trigger on Child Object after insert, update, delete event and update the parent filed

Thanks
Lokesh
DeveloperDeveloper
Hi Naveen,
try bellow code, please let me know useful or not.

01trigger UpdateOrder on Child__c (after insert, after update, after delete, after undelete) {
02 
03   List<Parent__c> ct = new List<Parent__c>();
04    
05   Set<Id> custord = new Set<Id>();
06    
07   if(Trigger.isDelete) {
08     for(Child__c test:Trigger.Old) {
09       
10        custord.add(test.Parent__c);  
11     
12     }  
13    
14   }
15   else
16   if(Trigger.isUpdate) {
17 
18     for(Child__c test:Trigger.New) {
19       
20        custord.add(test.Parent__c);  
21     
22     }
23 
24     for(Child__c test:Trigger.Old) {
25       
26        custord.add(test.Parent__c);  
27     
28     }  
29    
30   }
31   else
32   {
33     for(Child__c test:Trigger.New) {
34       
35        custord.add(test.Parent__c);  
36     
37     }
38   }
39    
40   AggregateResult[] groupedResults = [SELECT COUNT(Id), Parent__c FROM Child__c where Parent__c IN:custord GROUP      BY Parent__c ];
41    
42   for(AggregateResult ar:groupedResults) {
43      
44     Id custid = (ID)ar.get('Parent__c');
45      
46     Integer count = (INTEGER)ar.get('expr0');
47      
48     Parent__c cust1 = new Parent__c(Id=custid);
49      
50     cust1.child_count__c = count;
51      
52     ct.add(cust1);
53       
54   }
55    
56    
57   update ct;
58 
59}


Thanks & Regards  
Gopal 
Shiva RajendranShiva Rajendran
Hi naveen reddy,

Lets assume you have an parent object as ParentObject and childObject as childObject .
The below code must work.
trigger childTrigger(after insert,after update)
{
List<ChildOjbect> allChildObjects=[select id,amount__c from ChildOjbect where id in:Trigger.New];
Map<Id,ParentObject__c> allParent=new map<Id,ParentObject__c>()[select id,amount__c from ParentObject__c where id in:allChildObjects.Id];

     for(ChildObject oo :Trigger.new)
{
       double amount__c= oo.amount__c;  //childs amount;
      //assume ParentObject__c is the lookup in childobject
       ParenObject__c o= allParent.get(oo.ParentObject__c);
      o.amount__c+=amount__c;

}
update allParent.values();


}

Do modify this code to suit your requirement.Let me know if you face any further issues in the code.
Thanks and Regards,
Shiva RV

 
Mahalakshmi BRMahalakshmi BR
Hi,

Try this. It should work.


1) trigger Parentcount on Books__c (after insert , after delete) {
    
    List<Books__c> book = Trigger.isInsert ? Trigger.New : Trigger.old;
    list<id> b = new list<id>();
    for(Books__c bo : book)
    {
        b.add(bo.Callcountpar__c);
    }
    
    list<Parentobj__c> parent = [select id,(select id from Books__r),Call_Count1__c from Parentobj__c where id IN :b];
    for(Parentobj__c par : parent)
    {
        par.Call_Count1__c = par.Books__r.size();
    }
    update parent;
}

or this way

2) trigger Rollup on Price__c (after insert, after update, after delete) {
    
  set<Id> pro = new set<Id>();
    list<Product__c> pro1 = new list<Product__c>();
    
    for(Price__c p : Trigger.new)
    {
        pro.add(p.Id);
    }
    
    if(Trigger.isdelete || trigger.isupdate)
    {
        for(Price__c p : Trigger.old)
        {
            pro.add(p.id);
        }
    }
    
    list<Product__c> pr = new list<Product__c> ([select id,Product_Name__c,price__c,rollup__c,(select id from Price__r) from Product__c where id IN : pro]);
    for(Product__c p1 : pr)

     {
         pro1.add(p1);
         pro1.rollup__c = pro1.Price__r.Size();
     }
    

}

 
Vivek C 1Vivek C 1
Hi Naveen,

Try this, the below code works with the Account - Contact Relationship, where it captures total Number of Contacts for each Account
 
trigger RollupSummeryTrigger on Contact (after insert, after update, after delete, after undelete) {
    Set<id> aId = new Set<id>(); 
    List<Contact> contactTrigger = (trigger.isDelete)? trigger.old : trigger.new;
    for(Contact co : contactTrigger){
        aId.add(co.AccountId);
    }
    System.debug('AccountId : '+aId);
    if(aId != null && aId.size() > 0) {
        List<Account> accDetails = [SELECT id, Rollup_Summery_Trigger__c, (SELECT id FROM Contacts) FROM Account WHERE id in: aID];
        if(accDetails.size()> 0) {
            System.debug('accDetails.size() : '+accDetails.size());
             
            for(Account acc : accDetails){
                System.debug('Contacts.size() : '+acc.Contacts.size());
                acc.Rollup_Summery_Trigger__c = acc.Contacts.size();
            }
            Update accDetails;
        }
    }
}

Regards,
Vivek C​
Vivek C 1Vivek C 1
Hi Naveen,

Sorry i missed that amont field, if you want to display all the total amount values in Account. Where the total amount fields are calculated from the each account linked Contact.
 
trigger RollupSummeryTrigger on Contact (after insert, after update, after delete) {
    Set<id> aId = new Set<id>(); 
    List<Contact> contactTrigger = (trigger.isDelete)? trigger.old : trigger.new;
    for(Contact co : contactTrigger){
        aId.add(co.AccountId);
    }
    System.debug('AccountId : '+aId);
    if(aId != null && aId.size() > 0) {
        List<Account> accDetails = [SELECT id, Rollup_Summery_Trigger__c, (SELECT id, Amount_For_RollupSummery_Trigger__c FROM Contacts) FROM Account WHERE id in: aID];
        if(accDetails.size()> 0) {
            System.debug('accDetails.size() : '+accDetails[0]);  
            System.debug('accDetails.Contacts.size() : '+accDetails[0].Contacts.size());    
            for(integer i = 0; i < accDetails[0].Contacts.size(); i++) {
                for(Account acc : accDetails){
                	System.debug('Contacts.size() : '+acc.Contacts.get(i));
                	acc.Rollup_Summery_Trigger__c += acc.Contacts.get(i).Amount_For_RollupSummery_Trigger__c;
            	}            
            }
            Update accDetails;
        }
    }
}

Regards,
Vivek C​