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 

Account Trigger for Cross Object Roll Up

Can anyone help explain why my trigger wont fire?  I am really stumpped.  Any help is much appreciated. 

trigger ConsultingAUMRS on Account (before update, after update) { system.debug ('Raul');{
Account Rollup = new Account();
    for (AggregateResult ar:[select sum(AUM__C)total from Investment__c Where Consultant__c !=null])
{
  system.debug('Hello1');
      Rollup.Total_Consultant_AUM__c = (decimal)ar.get('total');
       system.debug((decimal)ar.get('total'));
   }
}
}
 
Forza di SognoForza di Sogno
It appears you're not operating on the records that are part of the trigger.
trigger ConsultingAUMRS on Account (before update) //, after update) 
{ 
	system.debug ('Raul');
	for (Account a : Trigger.new) 
	{ 
		//Account Rollup = new Account();
		for (AggregateResult ar:[select sum(AUM__C)total from Investment__c Where Consultant__c !=null])
		{
			system.debug('Hello1');
		  	a.Total_Consultant_AUM__c = (decimal)ar.get('total');
		   	system.debug((decimal)ar.get('total'));
		}
	
	} // for (Account a : Trigger.new) 	

}

 

 
Raul HaynesRaul Haynes
Thanks .... this is still not working I think because its wont allow my to pull the Account ID.  I am trying to , Jump to my Investment Object and umm all the active Investment then display that on my account obj.

trigger ConsultingAUMRS on Account (before update) 

        
   for (Account ac :Trigger.new)
   {
     string accountID;
     accountID = ac.Name;
     system.debug(accountID);
   }           
   //system.debug(accountID);

    for (Account a :Trigger.new)
    {
        //String accid = a.Name;
         
    for (AggregateResult ar:[select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Organization__c =: accountID]);
                {
                system.debug('Hello1');
                a.Total_Consultant_AUM__c = (decimal)ar.get('total');
                system.debug((decimal)ar.get('total'));
        } 
    }

}

 
Forza di SognoForza di Sogno
Is Organization__c a lookup field? In that case you'll need accountID = ac.ID, not accountID = ac.Name
Raul HaynesRaul Haynes
Yes its is a look up on the Investment Object. We are trying to capture the AccountID in a variable so that when we aggregate we are doing it for the current Account not all account 

Updated Code 
trigger ConsultingAUMRS on Account (before update) 

        
   for (Account ac :Trigger.new)
   {
     string accountID;
     accountID = ac.Name;
     system.debug(accountID);
   }           
   system.debug(accountID);

    /*for (Account a :Trigger.new)
    {
        //String accid = a.Name;
         
    for (AggregateResult ar:[select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Organization__c =: accountID]);
                {
                system.debug('Hello1');
                a.Total_Consultant_AUM__c = (decimal)ar.get('total');
                system.debug((decimal)ar.get('total'));
        } 
    }*/

}
Raul HaynesRaul Haynes
trigger ConsultingAUMRS on Account (before update) 

        
   for (Account ac :Trigger.new)
   {
     string accountID;
     accountID = ac.ID;
     //system.debug(accountID);
   }           
   system.debug(accountID);

    for (Account a :Trigger.new)
    {
        //String accid = a.Name;
         
    for (AggregateResult ar:[select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Organization__c =: accountID]);
                {
                system.debug('Hello1');
                a.Total_Consultant_AUM__c = (decimal)ar.get('total');
                system.debug((decimal)ar.get('total'));
        } 
    }

}
 
Forza di SognoForza di Sogno
Try this and check the debug logs to see the results.  Also have you check that the SOQL statement returns something in the Developer Console?
select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Organization__c =​ '<sample account id>';
trigger ConsultingAUMRS on Account (before update) 
{ 
  
System.debug('++++++++++++ before update trigger +++++++');
     
   for (Account ac :Trigger.new)
   {
System.debug('++++++++ found account: ' + ac.Name);
	   
     AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Organization__c =: ac.Id];
    
System.debug('++++++++ total (double) ' + (Double) ar.get('total'); 
System.debug('++++++++ total (decimal)' + (Decimal) ar.get('total');
         
     ac.Total_Consultant_AUM__c = (Double) ar.get('total');

   }     // for (Account ac :Trigger.new)      
   
}

 
Raul HaynesRaul Haynes
The Query is producing data however the debug is not working. I even tried formula field to pull in the Org ID. 

trigger ConsultingAUMRS on Account (before update) 

  
System.debug('++++++++++++ before update trigger +++++++');
     
   for (Account ac :Trigger.new)
   {
System.debug('++++++++ found account: ' + ac.ID);
       
     AggregateResult ar = [select sum(AUM__C)total from Investment__c Where Consultant__c !=null and Account_ID__c =: ac.Id];
    
//System.debug('++++++++ total (double) ' + (Double) ar.get('total'); 
//System.debug('++++++++ total (decimal)' + (Decimal) ar.get('total');
         
     ac.Total_Consultant_AUM__c = (Double) ar.get('total');

   }     // for (Account ac :Trigger.new)      
   
}

 
Forza di SognoForza di Sogno
Sorry, those 2 system.debug statements are missing a right parenthesis ) before the ; That should do the trick.