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

Help on trigger to create the total of transactions



I have two objects here





I have a field on Transactions named Amount

and a field on Achieved name Total Value


If a user does 7 transactions in a financial year , I want the total of the 7 transaction object  Amount field to add up to the Total Value field on Achieved object.


If the user deletes the transaction, the Total value field should be deducted also.


Financial year is 1 Jan 2012 - 31 DEC 2012. The transaction should be decided based on the created date.


Please help me on this



Niket SFNiket SF

Hello Smit,


           Trigger will be on Transactions object after insert and after update. In the actual trigger coding . You need to create a Map<Achieved,List<Transactions>>  


and then you need to process tha each and every tarnsaction. This is the core logic.

If you have actual code there you can post :)







What the relation between the Transction Object and Achieved Object ....


Hi SFDC_Evolve,


I have a look up relationship between the Transaction and Achieved object.


Unfortunately I cannot create a Master - Detail relationship as the Transaction object is a managed object.


Can you please help me with the trigger.


Thanks and KR


Hey Phantom,


The below code should help.


Trigger UpdateTransactions on Transactions__c (After Insert, After Update, After Delete) 
    Set<Id> AccountIds = new Set<Id>();
    list<Achieved__c> updateAccounts = new List<Account>();
    List<Sobject> transactions;

    if (Trigger.isAfter) 
        if (Trigger.isInsert || Trigger.isUpdate) 
            for(ATM_Transaction__c atm:
        if (Trigger.isDelete) 
            for(ATM_Transaction__c atm: trigger.old)
    transactions = [select account__c ar1, sum(amount__c) s from ATM_Transaction__c where account__c IN:AccountIds GROUP BY account__c];
    system.debug('Total -'+transactions);

    for (Sobject so:transactions)  
        system.debug('Inside Loop - '+so);
        AggregateResult ar = (AggregateResult) so;
        Integer counter = Integer.valueOf(ar.get('s'));
        Id AccountId = String.valueOf(ar.get('ar1'));
        Account updatetransactions = new Account (Id = AccountId);
        updatetransactions.Balance__c = counter;
            update updateAccounts;
        catch(Exception e)
            system.debug('Exception - '+e);


I have used Account Object as the Object on which the value needs to be rolled up. 


Transactions__c is the child object and Account__c is a lookup to the Account Object,


Let me know if this helps.


 hi phantom, i have a small query, please provide your email id. 
thanks and regards ]\