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
Supraja KallakuriSupraja Kallakuri 

Roll up summary on look up relation - after insert, after update, after delete, after undelete

I am trying to write a trigger to provide roll up summary on lookup relationships. Opportunity has a custom Account look up, in this case. The trigger should tell me how many deals are in expiring, expired stages and in total.

Here is my trigger.

trigger PAO on Opportunity (after delete,after update, after insert, after undelete) {
list<Account> Partneraccountlisttotal = New list<Account>();
list<Account> Partneraccountlistexpiring = New list<Account>();
list<Account> Partneraccountlistexpired = New list<Account>();    
set<id> partneraccountIDs = new set<id>();
List <AggregateResult> aggresultstotal = new List<AggregateResult>();
List <AggregateResult> aggresultsexpiring = new List<AggregateResult>();
List <AggregateResult> aggresultsexpired = new List<AggregateResult>();
  for(Opportunity  o :{
else if(trigger.isDelete){
    for(Opportunity  o: Trigger.old){
else if(trigger.isUnDelete){
  for(Opportunity  o :{
else if(trigger.isUpdate){
     for(Opportunity  o :{


aggresultstotal = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND 
              Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultstotal) {
      Partneraccountlisttotal.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Total__C = (Integer)ar.get('expr0')));
    aggresultsexpiring = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expiring__C = TRUE
                              AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpiring) {
       Partneraccountlistexpiring.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expiring__C = (Integer)ar.get('expr0')));
aggresultsexpired = [SELECT COUNT(Id), Partner_Account__c FROM Opportunity where Partner_Register__C = TRUE AND Deal_Expired__C = TRUE 
                             AND Partner_Account__c IN :partneraccountIDs GROUP BY Partner_Account__c];
   for(AggregateResult ar:aggresultsexpired) {
       Partneraccountlistexpired.add(new Account(
                Id = (ID)ar.get('Partner_Account__c'), 
                PAO_Expired__C = (Integer)ar.get('expr0')));

      update Partneraccountlisttotal; 
      update Partneraccountlistexpiring;
      update Partneraccountlistexpired;

And my test class-

public class TestPAO {
    Static testmethod void testpao(){
        Account a = New Account();
        a.Name = 'Test Account1';
        a.Account_Type2__c = 'Advisor';
        a.PAO_Expiring__c = 0;
        a.PAO_Total__C = 0;
        a.PAO_Expired__c =0;
        Insert a;
        Account b = New Account();
        b.Name = 'Test Account2';
        b.Account_Type2__c = 'Advisor';
        b.PAO_Expiring__c = 0;
        b.PAO_Total__C = 0;
        b.PAO_Expired__c =0;
        Insert b;
         Account a1 = New Account();
        a1.Name = 'Test Account3';
        a1.Account_Type2__c = 'Advisor';
        a1.PAO_Expiring__c = 0;
        a1.PAO_Total__C = 0;
        a1.PAO_Expired__c =0;
        Insert a1;
        Account b1 = New Account();
        b1.Name = 'Test Account4';
        b1.Account_Type2__c = 'Advisor';
        b1.PAO_Expiring__c = 0;
        b1.PAO_Total__C = 0;
        b1.PAO_Expired__c =0;
        Insert b1;
        Opportunity o = New Opportunity();
        o.Name = 'Test opportunity';
        o.Account = a;
        o.Partner_Account__c = a1.ID;
        o.Type = 'New Business';
        o.CloseDate = system.Today().addDays(10);
        o.StageName ='Negotiating';
        o.Partner_Register__C = TRUE;
        o.Deal_Expiring__c = False;
        o.Deal_Expired__C = False;
        Insert o;
        Opportunity o2 = New Opportunity();
        o2.Name = 'Test opportunity';
        o2.Account = b;
        o2.Partner_Account__c = b1.ID;
        o2.Type = 'New Business';
        o2.CloseDate = system.Today().addDays(10);
        o2.StageName ='Negotiating';
        o2.Partner_Register__C = TRUE;
        o2.Deal_Expiring__c = True;
        o2.Deal_Expired__c = False;
        Insert o2;
        List<Account> test1 = [Select ID FROM Account WHERE PAO_Total__C = 1];
        List<Account> test2 = [Select ID FROM Account WHERE PAO_Expiring__C = 1];
        List<Account> test3 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        system.assertEquals(test1.size(), 2);
        system.assertEquals(test2.size(), 1);
        system.assertEquals(test3.size(), 3);
        o2.Deal_Expiring__c = False;
        update o2;
        List<Account> test4 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test5 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        system.assertEquals(test4.size(), 4);
        system.assertEquals(test5.size(), 1);
        delete o2;
        List<Account> test6 = [Select ID FROM Account WHERE PAO_Expiring__C = 0];
        List<Account> test7 = [Select ID FROM Account WHERE PAO_Expired__C = 1];
        system.assertEquals(test6.size(), 4);
        system.assertEquals(test7.size(), 0);


Issue - System.AssertException: Assertion Failed

Any suggestions would be highly appreciated. Thanks!


Just try to include system.debug in your test class and check the size of the test1,test2,test3,test6 and test7 and based on the values put system.assertEquals.

Supraja KallakuriSupraja Kallakuri
Thanks for your time, Patcs_1. But the idea is that system.assertEquals tells me if the trigger is working the way i intend it to or not.
I understood that, if you want to figure it out, you can have system.debug to sort out the issues. you can see logs and it will make you understand why the excepted record is not updated.

Supraja KallakuriSupraja Kallakuri
Here is where it fails. When I change the deal_Expiring__C or Deal_Expired__C from TRUE to false, it fails. If i do it otherwise it works fine. Also, when I delete, it would not update the PAO_Expiring or PAO_Total or PAO_Expired. (Technically it hould reduce the numbers)