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
Ashish Agrawal 4Ashish Agrawal 4 

Need help to store aggregateresults value into custom field total_expenses__c

Hi developers,
Need help i am stuck with how to get total expenses with aggregateresults as of now i done with the aggregateresults value but not able to store in custom field  i.e total_expenses__c

mycode is:
trigger total_expenses on DailyExpense__c (before insert,after insert)
{
Integer totalexpense;

DailyExpense__c de  = new DailyExpense__c();
list<AggregateResult> ar = new list<AggregateResult>();


if(trigger.Isbefore)
{
   for(DailyExpense__c de1 :trigger.new)
   {
    de1.Total_Monthly_income__c =25000;
   }
}
  ar = [select sum(Amount__c)amt from DailyExpense__C Group by CALENDAR_MONTH(Dates__c)];

for( AggregateResult obj:ar)
{

   totalexpense = Integer.valueOf(obj.get('amt'));
   de.Total_expenses__c = totalexpense;
 
}
  // update de;
}

Regrads,
ashish
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger total_expenses on DailyExpense__c (before insert,after insert)
{
DailyExpense__c de  = new DailyExpense__c();
if(trigger.Isbefore)
{
for(DailyExpense__c de1 :trigger.new)
{
    de1.Total_Monthly_income__c =25000;
}
}
list<AggregateResult> ar = [select sum(Amount__c)amt from DailyExpense__C Group by CALENDAR_MONTH(Dates__c)];
for( AggregateResult obj:ar)
{
Integer totalexpense = obj.get('amt');
de.Total_expenses__c = totalexpense;
}
//Try this code
James LoghryJames Loghry
Why not use a Rollup Summary Field for this purpose instead of implementing a custom trigger?  https://help.salesforce.com/HTViewHelpDoc?id=fields_about_roll_up_summary_fields.htm&language=en_US (https://help.salesforce.com/HTViewHelpDoc?id=fields_about_roll_up_summary_fields.htm&language=en_US)
Ashish Agrawal 4Ashish Agrawal 4
hi Vidhyasagaran Muralidharan,

Thans for the reply, i tried using u r code but we are able to save a trigger but in detail page its not stored in customfield its not showing.
trigger total_expenses on DailyExpense__c (before insert,after insert)
{
DailyExpense__c de  = new DailyExpense__c();
if(trigger.Isbefore)
{
for(DailyExpense__c de1 :trigger.new)
{
    de1.Total_Monthly_income__c =25000;
}
}
list<AggregateResult> ar = [select sum(Amount__c)amt from DailyExpense__C Group by CALENDAR_MONTH(Dates__c)];
for( AggregateResult obj:ar)
{
Integer totalexpense = Integer.valueOf(obj.get('amt'));
system.debug('total integer expenses ' + totalexpense);
de.Total_expenses__c = totalexpense;
system.debug('total Amount expenses ' + de.Total_expenses__c);
}

}
Ashish Agrawal 4Ashish Agrawal 4
i think we need to use some insert or update command in trigger