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
brozinickrbrozinickr 

Bulkify Roll Up Summary Trigger

Hello,

 

I was wondering if someone would be able to help me bulifying the trigger below.  As of now, this trigger counts all of the calls made in one month on a particular account made by a particular system profile.

 

I just was given the requirements that I need to do the same thing but for another profile, AL - AM Rep and write to a different field, AM_Activity_Count__c.  I know I could just duplicate this trigger, change the name in the soql, and then save it.  But I know there has to be a way that I can bulkify this.  I've been messing around trying to get this to work and I've been unable to get it to work so far.  Here't what I have originally:

 

 

trigger Activity_Count on Task (after insert, after update, after delete) 
{
    List<Id> taskIds = new List<Id>();
    List<Id> accIds = new List<Id>();
    Map<Id,Account> accMap = new Map<Id,Account>();
    List<Account> updateList = new List<Account>();  // edited
    String accPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    
    for (Task t : Trigger.isDelete ? Trigger.old : Trigger.new)
    {
        taskIds.add(t.Id);
        String str = t.whatId;
        if(str != null && str.startsWith(accPrefix))     // Task belongs to Account
          {
            accIds.add(t.whatId);
          }
    }
    
    if(accIds.size() > 0)
    {
        for(Account ac : [SELECT Id, Activity_Count__c FROM Account WHERE Id in :accIds])
        {
            accMap.put(ac.Id,ac);
        }

        for(AggregateResult ar : [SELECT Count(Id) c, WhatId w, Owner.Profile.Name
                                  FROM Task
                                  WHERE whatId IN :accIds
                                  AND Subject Like 'Call%'
                                  AND CreatedDate = This_Month
                                  AND Owner.Profile.Name = 'AL - AS Rep'
                                  GROUP BY WhatId, Owner.Profile.Name])
        {
            Account acc = new Account();
            
            string str = string.valueOf(ar.get('w'));
            
            acc = accMap.get(str);   // Edited   //
            acc.Activity_Count__c = Integer.valueOf(ar.get('c'));   // Edited
            
            updateList.add(acc);
        }
    }
    
    if(updateList.size() > 0)
    {
        update updateList;
    
    }
}
Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger Activity_Count on Task (after insert, after update, after delete) 
{
    List<Id> taskIds = new List<Id>();
    List<Id> accIds = new List<Id>();
    Map<Id,Account> accMap = new Map<Id,Account>();
    List<Account> updateList = new List<Account>();  // edited
    String accPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    
    for (Task t : Trigger.isDelete ? Trigger.old : Trigger.new)
    {
        taskIds.add(t.Id);
        String str = t.whatId;
        if(str != null && str.startsWith(accPrefix))     // Task belongs to Account
          {
            accIds.add(t.whatId);
          }
    }
    
    if(accIds.size() > 0)
    {
        accMap = new Map<Id, Account> ([SELECT Id, Activity_Count__c FROM Account WHERE Id in :accIds]);

        for(AggregateResult ar : [SELECT Count(Id) c, WhatId w, Owner.Profile.Name Pf
                                  FROM Task
                                  WHERE whatId IN :accIds
                                  AND Subject Like 'Call%'
                                  AND CreatedDate = This_Month
                                  AND (Owner.Profile.Name = 'AL - AS Rep' OR Owner.Profile.Name = 'AL - AM Rep')
                                  GROUP BY WhatId, Owner.Profile.Name])
        {
            Account acc = new Account();
            
            acc = accMap.get(String.valueOf(ar.get('w')));

            if(String.valueOf(ar.get('pf')) == 'AL - AS Rep')
            {
              acc.Activity_Count__c = Integer.valueOf(ar.get('c'));  
            }
            else
            {
              acc.AM_Activity_Count__c = Integer.valueOf(ar.get('c'));
            }
            
            accMap.put(acc.Id, acc);
}
}

updateList = accMap.values();

if(updateList.size() > 0)
{
update updateList;
}

}

 try this.

All Answers

Naidu PothiniNaidu Pothini
trigger Activity_Count on Task (after insert, after update, after delete) 
{
    List<Id> taskIds = new List<Id>();
    List<Id> accIds = new List<Id>();
    Map<Id,Account> accMap = new Map<Id,Account>();
    List<Account> updateList = new List<Account>();  // edited
    String accPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    
    for (Task t : Trigger.isDelete ? Trigger.old : Trigger.new)
    {
        taskIds.add(t.Id);
        String str = t.whatId;
        if(str != null && str.startsWith(accPrefix))     // Task belongs to Account
          {
            accIds.add(t.whatId);
          }
    }
    
    if(accIds.size() > 0)
    {
        accMap = new Map<Id, Account> ([SELECT Id, Activity_Count__c FROM Account WHERE Id in :accIds]);

        for(AggregateResult ar : [SELECT Count(Id) c, WhatId w, Owner.Profile.Name Pf
                                  FROM Task
                                  WHERE whatId IN :accIds
                                  AND Subject Like 'Call%'
                                  AND CreatedDate = This_Month
                                  AND (Owner.Profile.Name = 'AL - AS Rep' OR Owner.Profile.Name = 'AL - AM Rep')
                                  GROUP BY WhatId, Owner.Profile.Name])
        {
            Account acc = new Account();
            
            acc = accMap.get(String.valueOf(ar.get('w')));

            if(String.valueOf(ar.get('pf')) == 'AL - AS Rep')
            {
              acc.Activity_Count__c = Integer.valueOf(ar.get('c'));  
            }
            else
            {
              acc.AM_Activity_Count__c = Integer.valueOf(ar.get('c'));
            }
            
            accMap.put(acc.Id, acc);
}
}

updateList = accMap.values();

if(updateList.size() > 0)
{
update updateList;
}

}

 try this.

This was selected as the best answer
brozinickrbrozinickr

Thanks Naidu for your help!

 

I just tested it and doesn't seem to like the line:

 

updateList = acc.values();

 

Here's the error it gives:

 

 Error: Compile Error: Method does not exist or incorrect signature: acc.values() at line 53 column 18
Naidu PothiniNaidu Pothini

Updated the code. try it.

brozinickrbrozinickr

No dice.  It's still failing on the same line with the same error. :(   Do the curly brackets starting before the line Account acc = new Account(); need to be removed?

 

Naidu PothiniNaidu Pothini

try now. I forgot to update the code where i am bringing the values from the accMap.

 

brozinickrbrozinickr

Perfect!  Thanks Naidu! :)

Vrajesh ShethVrajesh Sheth
We need to take care of undelete event as well. Here is the code which take care of all the events for roll up summary

trigger AddivantPriceRecordItemTrigger on Price_Record_Item__c (after insert, after update,after delete, after undelete) {
//Variables
set<id> parentRecordIDset = new set<id>();
list<Price_Record__c> parentRecordlist = new list<Price_Record__c>();
list<Price_Record_Item__c> childRecordList = new list<Price_Record_Item__c>();

//Get the parent record id from child record
for(Price_Record_Item__c childrecord : ((!trigger.isdelete)?trigger.new:trigger.old)){
  parentRecordIDset.add(childrecord.Price_Record__c);
}
//Get parent record list
parentRecordlist = [SELECT id
          ,Items_Status__c
      FROM Price_Record__c
      WHERE id in :parentRecordIDset];

//get child record list
childRecordList = [SELECT id
       ,Status__c
       ,Price_Record__c
        FROM Price_Record_Item__c
        WHERE Price_Record__c in :parentRecordIDset];
 
//Apply the logic in this loop    
for(Price_Record__c parentrecord : parentRecordlist){
  //Reset the rollup fields for parent record
  parentrecord.Item_Status__c = '';
  for(Price_Record_Item__c childrecord : childRecordList){
   if(parentrecord.id == childrecord.Price_Record__c){
    //Put your roll up logic here
    parentrecord.Item_Status__c = parentrecord.Item_Status__c + childrecord.Status__c + ',';
   }
  }
}

//Update parent record
update parentRecordlist;    
}//EOF