+ Start a Discussion
Mariappan PerumalMariappan Perumal 

Doubt - Bulkification of wonderful trigger

Hi Everyone ,

 

Nice to come with the wonderful doubt to the board.

 

I have written a trigger which is perform the rollup operation in lookup relation between 2 objects , I mean only counts the child records . 

 

I could have gone with the rollup summary field here but because of requirements , I wrote the trigger , the things here is like , we need to count the accounts which are all associated to single opportunity . 

 

Here is trigger i came up with  :

 

trigger OpportunityRollup on Account (after insert, after update, 
                                        before delete, after undelete) {

if(Trigger.isInsert)
{
Set<Id>oppids=new Set<Id>();
List<Opportunity> oplist=new List<Opportunity>();
List<Opportunity> oppo=new List<Opportunity>();
    For(Account acc: Trigger.New)
    {
    if(acc.RecordTypeId=='012R0000000D9iF' && acc.Opportunity__c!=null)
    {
    oppids.add(acc.Opportunity__c);
    }
    }
    oppo =[select Actual_Student_Accounts__c from Opportunity where Id IN: oppids];
    For(Opportunity oo:oppo)
    {
    oo.Actual_Student_Accounts__c=oo.Actual_Student_Accounts__c+1;
    oplist.add(oo);
    }
    
    update oplist;

}

}

 

The trigger will fine , it bulkified by making query and dml outside of loop .

 

My quuestion here is :

Whenever a account is inserted , it will increament the count field in opportunity to which it is associated, 

so 2 accounts which are associated to 2 different accounts will increament the counter field in the opportunity to which it is associated respectively ,

 

But if 2 accounts which are associated to same accounts , since set is used to hold the opportunity id of the account , it increaments the counter field once . I tried with the List instead of Set . 

 

What should we do to make it work perfectly ?

 

Thanks in advance
    

Best Answer chosen by Admin (Salesforce Developers) 
Mariappan PerumalMariappan Perumal

Hi Everyone 

 

Got the wonderful answero for my question and most best way of doing that.

 

http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/

All Answers

rbohnrbohn

I'm not sure of the answer to your APEX question but another approach to solving your rollup if Rollup Summary does not work would be to look at the AppExchange program Rollup Helper.    It works any many use cases where Rollup Summary fields do not work.

 

https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI

 

Team WorksTeam Works

Try!

oppo =[select Actual_Student_Accounts__c,(Select Id from Accounts) from Opportunity where Id IN: oppids];
    For(Opportunity oo:oppo)
    {

      for(Account acc : oo.Accounts){
       oo.Actual_Student_Accounts__c=oo.Actual_Student_Accounts__c+1;
       oplist.add(oo);
    }

}

Mariappan PerumalMariappan Perumal

Hi ,

How are you doing ? and It really great to hear from you.

 

Here too, we are using set and so it won't  allow duplicate id right. 

 

What should we do in this case , it really really confusing me  how to proceed with this. 

Mariappan PerumalMariappan Perumal

Hi Everyone 

 

Got the wonderful answero for my question and most best way of doing that.

 

http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/

This was selected as the best answer