+ Start a Discussion
sonam gupthasonam guptha 

update is not working on existing records for sumof values

Hi,

Here iam adding sum of revenue__c to account column acc.Finance__c.
for Example :-- 
 if  opportunity.member__c.Revenue__c is 10 
 & opportunity.member__c.Revenue__c is 20
 total = 30 
 this value iam printing on acc.Finance__c column,but now the problem is its keep on adding the values when we are changing the value on same record.
 for example :--
 for the same above record the value is 30.
 if i change the same opportunity.member__c.Revenue__c is 10 to 20
 & opportunity.member__c.Revenue__c is 20
 the value should be 40 but here it counting has 50.
 its counting the old value even.
 can any one help me to resolve it??
trigger updateAccount on member__c (After Insert,After update,After Delete,) {
    
    List<Account> UpdateList = new List<Account>();

    Id recTypeFinance = [select Id,name from RecordType where name='Finance' and SObjectType='member__c' limit 1].Id;
    
    string ids;
    if(Trigger.isInsert || Trigger.isUpdate)
    {
        for(Asset__c asst : Trigger.new) {
            if(asst.Opportunity__c != null)
                ids= asst.id;
        }
        
        For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){     
            
            if(ast.RecordTypeId == recTypeFinance){
         for(opportunity opp : [Select Id,AccountId from Opportunity where Id =: ast.Opportunity__c]){
                    for(Account acc : [Select Id,Finance__c from Account where Id =: opp.AccountId]){
                        if(acc.Finance__c == null){
                            acc.Finance__c= ast.Revenue__c;
                        }else{
                            acc.Finance__c += ast.Revenue__c;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
     }

 
HARSHIL U PARIKHHARSHIL U PARIKH
I can offer the trigger below,

See on line 25 - If I don't set coming Con.Gorss_Amount_Total__c = 0.00 then it will add the new total on top of old one. Hope you can relate this to your trigger.
Trigger Code:
Trigger SummingUpGrossAmountOnContact on Credit__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> conIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Credit__c Credit : Trigger.New){
            If(Credit.Contact__c != null){
                conIds.add(Credit.Contact__c);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Credit__c Credit : Trigger.Old){
            If(Credit.Contact__c != null){
                conIds.add(Credit.Contact__c);
            }
        }
    }
    
    List<Contact> ConFinalListToUpdate = New List<Contact>();
    
    For(Contact Con : [Select Id, Gorss_Amount_Total__c, 
                                (Select Id, Gross_Amount__c FROM Credits__r WHERE Gross_Amount__c != null) 
                                                            FROM Contact WHERE Id =:conIds ]){
        Con.Gorss_Amount_Total__c = 0.00;
        For(Credit__c EveryCredit : Con.Credits__r)
        {
            Con.Gorss_Amount_Total__c  += EveryCredit.Gross_Amount__c;
        }
        ConFinalListToUpdate.add(Con);
    }
    
    try{
        If(!ConFinalListToUpdate.IsEmpty()){
            update ConFinalListToUpdate;
        }
    }
    Catch(Exception e){
        System.debug('Thrown Exception While Updating Contacts:: ' + e.getMessage());
    }
}
Hope this helps!
 
sonam gupthasonam guptha
hey govind,

The same thing iam doing on my above my code,here my problem is when ever i update the same existing record with different value,its adding the previous values to total,i need to calculate with the latest value.

for example :--

i entered a member record with value 5 

second member record  with value 10
total value should be 15,this is working fine.

When you go and change the second member record once again, from 10 to 20
Total value should be 5(first member record value)+20(second member latest value) = 25

But here its calculating like 5+10(previous value)+20 (latest value) = 35

this is something we need to modify our code using trigger.old when updating it again?
HARSHIL U PARIKHHARSHIL U PARIKH
Wait.... I have just tested the trigger I have sent you and its working fine though.. It is showing 25, not 35. And of course, if you take both of the records off, it shows zero as well.