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
Raul HaynesRaul Haynes 

How to pass the Account ID for this trigger?

Hi I am looking to do a rollup of my Investment object based on the Account ID I select for some reason its not working. If I hard code the Account ID as a test it works correctly. Can anyone help me get past this issue? 

trigger ConsultingAUMRS on Account (before update)

  
System.debug('++++++++++++ before update trigger +++++++');
     
    
    for (Account a :Trigger.new)
    {
     
           set<Id> acctIds = new set<Id>();
           acctIds.add(a.Id);
           
    
       
     AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Org_ID__c =:acctIds];
    
System.debug('++++++++ total (double) ' + (Double) ar.get('total')); 
        
     a.Total_Consultant_AUM__c = (Double) ar.get('total');


   }
}
Arunkumar RArunkumar R
Hi Raul,

You can have a below link example it might help to you.

http://salesforcekings.blogspot.in/2015/07/rollup-summar-trigger-for-lookup.html
ManojjenaManojjena
HI Raul,

Can you please explain your requirment with bit more clarity for which we can give you better solution .
Todd GillTodd Gill
Raul,

As your code is written, you are performing a SOQL query for each record passed to the trigger, which is not best practice and could cause you to hit governor limits on mass updates.  You should correct this.

The reason you are not seeing results is that you are using the SOQL "=" operator with a set object (acctIds).  The following will return results with your code as written:

AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Org_ID__c =:a.Id];  //You are not using the Set<Id> acctIds object at all if you keep this SOQL query inside your loop

However, you should move your SOQL query outside the initial loop and it should use the set object and only one SOQL query to get the aggregate values for your rollup.  The example Arunkumar provided illustrates this approach.  I suggest you study some more examples and read/revew "bulkification" best practices.
Vishal_GuptaVishal_Gupta
Hi Raul,

If you have masterdetail relationship between Investment__c  and Account, you can create a rollup summary field directly on Account, which will maintain the total of AUM__C of all child Investment__c  records of a single Account.

Please let me know if I can help you more.

Thanks,
Vishal
William TranWilliam Tran
Replace this 

     AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Org_ID__c =:acctIds];


with this

     AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Org_ID__c  IN :acctIds];


As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks