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
sonam gupthasonam guptha 

apex trigger show total sum as wrong value on updating the previous records

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 from 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;
     }

 
Best Answer chosen by sonam guptha
v varaprasadv varaprasad
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.isAfter){
    if(Trigger.isInsert)
    {
        for(member__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 = acc.Finance__c + ast.Revenue__c;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
     }
	 
	 if(trigger.idUpdate){
	 
	 for(member__c asst : Trigger.new) {
            if(asst.Opportunity__c != null)
                ids= asst.id;
        }
        
		map<id,member__c> oldmap = trigger.oldmap();
		
		double revenue = null;
		
        For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){               
			   revenue = oldmap.get(ast.id).Revenue__c;
			   system.debug('===old revenue==='+revenue);
			   
            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 = acc.Finance__c + ast.Revenue__c - revenue;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
	 
	 }
	 }

 

All Answers

v varaprasadv varaprasad
Hi Soanam,

In above trigger is on member__C object but your mentioned triiger.new on Asset__c.

        for(Asset__c asst : Trigger.new) {
            if(asst.Opportunity__c != null)
                ids= asst.id;
        }

could you please confirm why we are using Asset here.

Thanks
Varaprasad
sonam gupthasonam guptha
sorry its member__c not asset__c,here is the code once again!
 
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(member__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;
     }

 
sonam gupthasonam guptha
Varaprasad,

Any clues?
v varaprasadv varaprasad
Hi Sonam,

Please check once below code : 
 
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(member__c asst : Trigger.new) {
            if(asst.Opportunity__c != null)
                ids= asst.id;
        }
        
		map<id,member__c> oldmap = trigger.oldmap();
		
		double revenue;
		
        For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){               
			   revenue = oldmap.get(ast.id).Revenue__c;
			   system.debug('===old revenue==='+revenue);
			   
            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;
							acc.Finance__c = acc.Finance__c + ast.Revenue__c - revenue;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
     }

Thanks
Varaprasad​
sonam gupthasonam guptha
varaprasad,

on line 15 it was giving me a compile error,so i removed braces of oldmap​(),and now its adding extra 1 number to total.
example :-- if i enter 10 and updated it,it was adding 11 to total,and its not working as we expected,can you please have a look
map<id,member__c> oldmap = trigger.oldmap();

changed to :--

map<id,member__c> oldmap = trigger.oldmap;

 
v varaprasadv varaprasad
remove below line : 

acc.Finance__c += ast.Revenue__c;
sonam gupthasonam guptha
After removing the acc.Finance__c += ast.Revenue__c
its working for updated records now, but for new records its giving me below error
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateAccount caused an unexpected exception, contact your administrator: updateAccount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateAccount: line 2o, column 1

 
v varaprasadv varaprasad
system.debug('==acc.Finance__c==' +acc.Finance__c+ '==ast.Revenue__c=='+ast.Revenue__c+ '==revenue=='+revenue);

Please add above line in else condition check once debug logs.


Thanks
Varaprasad
v varaprasadv varaprasad
if(Trigger.isUpdate){
  revenue = oldmap.get(ast.id).Revenue__c;
  system.debug('===old revenue==='+revenue);
}
add above line and check once.
v varaprasadv varaprasad
declare : 

double revenue = '';
sonam gupthasonam guptha
receving an error,and the exection stops at revenue = oldmap.get(ast.id).Revenue__c.
Illegal assignment from String to Double at line 21 column 16
i hope we are missing logic for trigger.new statement,it doesnt adding any value when we are entering new record?may be iam wrong but just an expectation
 
v varaprasadv varaprasad
revenue = double.valueOf(oldmap.get(ast.id).Revenue__c);
sonam gupthasonam guptha
still iam seeing the same error,and its not accepting double revenue = '';
v varaprasadv varaprasad
double revenue = null;
sonam gupthasonam guptha
same error i changed it to null
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger updateAccount caused an unexpected exception, contact your administrator: updateAccount: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.updateAccount: line 20, column 1
v varaprasadv varaprasad
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.isAfter){
    if(Trigger.isInsert)
    {
        for(member__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 = acc.Finance__c + ast.Revenue__c;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
     }
	 
	 if(trigger.idUpdate){
	 
	 for(member__c asst : Trigger.new) {
            if(asst.Opportunity__c != null)
                ids= asst.id;
        }
        
		map<id,member__c> oldmap = trigger.oldmap();
		
		double revenue = null;
		
        For(member__c ast:[select Id,Opportunity__c,Revenue__c,RecordTypeId from member__c where ID=:ids]){               
			   revenue = oldmap.get(ast.id).Revenue__c;
			   system.debug('===old revenue==='+revenue);
			   
            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 = acc.Finance__c + ast.Revenue__c - revenue;
                        }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
	 
	 }
	 }

 
This was selected as the best answer
Prathyu bPrathyu b
Hi Sonam,

Try below and let me know if it works


trigger accUpdate 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.isAfter && Trigger.isInsert || Trigger.isUpdate)    {
        for(member__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]){           
            system.debug('ast '+ast);
            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
                            if (Trigger.isUpdate ){
                                Decimal revn= Trigger.oldMap.get(ast.Id).Revenue__c;
                                if(revn != ast.Revenue__c)                                  
                                acc.Finance__c =acc.Finance__c - revn + ast.Revenue__c;
                            }else{
                                acc.Finance__c += ast.Revenue__c;
                            }
                        UpdateList.add(acc);
                    }
                }
            }
        }
        update UpdateList;
    }
}

Thanks
Ahmad J. KoubeissyAhmad J. Koubeissy
So what you are trying to do is calculating the revenue of all opportunities related to an account. like if you have 3 opportunities related to one account, you want to see the sum of revenue os these 3 opportunities on the account level ?
sonam gupthasonam guptha
thankyou varaprasad,its working now but here i have a workflow which is firing on the account how can we control that?