+ Start a Discussion
phantom1982phantom1982 

Help on trigger to create the total of transactions

Hi

 

I have two objects here

 

1)Transactions

2)Achieved

 

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

 

Thanks

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 :)

 

 

Thanks

Nik

 

SFDC_EvolveSFDC_Evolve

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

phantom1982phantom1982

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


sanjaypatidarsanjaypatidar

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: trigger.new)
                {
                AccountIds.add(atm.Account__c);
                }
            }
        if (Trigger.isDelete) 
            {
            for(ATM_Transaction__c atm: trigger.old)
                {
                AccountIds.add(atm.Account__c);
                }
            }
        }
            
    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;
        updateAccounts.add(updatetransactions);
        }
          
    if(!updateAccounts.isEmpty())
        {
        try
            {
            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.

 

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