+ Start a Discussion
Jim MontgomeryJim Montgomery 

Trigger not updating account

I hav ethis trigger that should update the parent account with the sum of number of documents from the child AC_Trial object. It is not updating the account.

trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {

    
    List<Account> list_Account= new List<Account>();
    set<Id> set_ACTrial = new set<Id>();
    for(AC_Trial__c objACT: trigger.new){
        set_ACTrial.add(objACT.Account__c);
    }
    Decimal Sum;
    for(Account objAccount : [SELECT Id,Name,(SELECT Id,Name,Number_Of_Documents__c FROM AC_Trial__r) FROM Account WHERE Id IN: set_ACTrial]){
        Sum=0;
        for(AC_Trial__c objACT01: objAccount.AC_Trial__r ){
            Sum+=objACT01.Number_Of_Documents__c ;
        }
        objAccount.AC_Trial_Documents__c=Sum;
        list_Account.add(objAccount);
    }
    update list_Account;
}
Best Answer chosen by Jim Montgomery
Steven NsubugaSteven Nsubuga
This should fix that.
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    if(trigger.isInsert || trigger.isupdate){
    for(AC_Trial__c objACT: trigger.new){
        if (objACT.Account__c != null) {
            set_ACTrial.add(objACT.Account__c);
        }
    }
    }
    if(Trigger.isDelete){
        for(AC_Trial__c objACT: Trigger.old){
            if (objACT.Account__c != null) {
                set_ACTrial.add(objACT.Account__c);
            }
        }
    }
    Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
	for (Id acctId : accountsMap.keyset()) {
        accountsMap.get(acctId).AC_Trial_Documents__c = 0;
    }
	
    List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
    
    for (AggregateResult ar : ars) {
        accountsMap.get(String.valueOf(ar.get('Account__c'))).AC_Trial_Documents__c = Integer.valueOf(ar.get('expr0'));
    }
    update accountsMap.values();
}

 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Jim,

I trust you are doing very well.

Please try below code:
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {

    
    List<Account> list_Account= new List<Account>();
    set<Id> set_ACTrial = new set<Id>();

    for(AC_Trial__c objACT: trigger.new){
        set_ACTrial.add(objACT.Account__c);
    }

    Decimal Sum;

    for(Account objAccount : [SELECT Id,Name, AC_Trial_Documents__c FROM Account WHERE Id IN: set_ACTrial]){
        Sum=0;
        for(AC_Trial__c objACT01: trigger.new){
            Sum+=objACT01.Number_Of_Documents__c ;
            objAccount.AC_Trial_Documents__c=Sum;
            list_Account.add(objAccount);
        }
    }
    update list_Account;
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Steven NsubugaSteven Nsubuga
I have rewritten parts of your trigger, also considering that you have a delete part.
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    for(AC_Trial__c objACT: trigger.new){
        set_ACTrial.add(objACT.Account__c);
    }
    if(Trigger.isDelete){
  for(AC_Trial__c objACT: Trigger.old){
   set_ACTrial.add(objACT.Account__c);
  }
 }
 Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
 List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
 
 for (AggregateResult ar : ars) {
  accountsMap.get(String.valueOf(ar.get('Account__c'))).Total_Lease_Area_SF_from_Children__c = Integer.valueOf(ar.get('expr0'));
 }
 update accountsMap.values();
}

 
Jim MontgomeryJim Montgomery
Khan, I tried you code but it is still not updating the account.
Steven, I am getting an error Total_Lease_Area_SF_from_Childern does not exist.
Steven NsubugaSteven Nsubuga
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    for(AC_Trial__c objACT: trigger.new){
        set_ACTrial.add(objACT.Account__c);
    }
    if(Trigger.isDelete){
  for(AC_Trial__c objACT: Trigger.old){
   set_ACTrial.add(objACT.Account__c);
  }
 }
 Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
 List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
 
 for (AggregateResult ar : ars) {
  accountsMap.get(String.valueOf(ar.get('Account__c'))).AC_Trial_Documents__c = Integer.valueOf(ar.get('expr0'));
 }
 update accountsMap.values();
}
Raj VakatiRaj Vakati
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    for(AC_Trial__c objACT: trigger.new){
        set_ACTrial.add(objACT.Account__c);
    }
    if(Trigger.isDelete){
  for(AC_Trial__c objACT: Trigger.old){
   set_ACTrial.add(objACT.Account__c);
  }
 }
 Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
 List<AggregateResult> ars = [SELECT Account__c acc, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
 
 for (AggregateResult ar : ars) {
  accountsMap.get(String.valueOf(ar.get('acc'))).Total_Lease_Area_SF_from_Children__c = Integer.valueOf(ar.get('expr0'));
 }
 update accountsMap.values();
}

 
Jim MontgomeryJim Montgomery
Setven, getting this error when saving an AC Trial record.

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ACTrialRollup caused an unexpected exception, contact your administrator: ACTrialRollup: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.ACTrialRollup: line 15, column 1
Steven NsubugaSteven Nsubuga
That error is due to the existence of some AC Trial records that do nothave the Lookup field Account__c populated.
Here is the updated trigger.
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    for(AC_Trial__c objACT: trigger.new){
        if (objACT.Account__c != null) {
            set_ACTrial.add(objACT.Account__c);
        }
    }
    if(Trigger.isDelete){
        for(AC_Trial__c objACT: Trigger.old){
            if (objACT.Account__c != null) {
                set_ACTrial.add(objACT.Account__c);
            }
        }
    }
    Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
    List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
    
    for (AggregateResult ar : ars) {
        accountsMap.get(String.valueOf(ar.get('Account__c'))).AC_Trial_Documents__c = Integer.valueOf(ar.get('expr0'));
    }
    update accountsMap.values();
}

 
Jim MontgomeryJim Montgomery
Steven, tried the latest code. No error but it doesn't update the account.
Jim MontgomeryJim Montgomery
I apologize, there were 2 account lookups i=on the object.
I had to add if statements for isUpdate and IsInsert due to an error.
Now, the only issue left is it is not setting the amount to zero when the only one AC_Trial record is deleted.

trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    if(trigger.isInsert || trigger.isupdate){
    for(AC_Trial__c objACT: trigger.new){
        if (objACT.Account__c != null) {
            set_ACTrial.add(objACT.Account__c);
        }
    }
    }
    if(Trigger.isDelete){
        for(AC_Trial__c objACT: Trigger.old){
            if (objACT.Account__c != null) {
                set_ACTrial.add(objACT.Account__c);
            }
        }
    }
    Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
    List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
    
    for (AggregateResult ar : ars) {
        accountsMap.get(String.valueOf(ar.get('Account__c'))).AC_Trial_Documents__c = Integer.valueOf(ar.get('expr0'));
    }
    update accountsMap.values();
}
Steven NsubugaSteven Nsubuga
This should fix that.
trigger ACTrialRollup on AC_Trial__c (after insert, after update, after delete) {
    set<Id> set_ACTrial = new set<Id>();
    if(trigger.isInsert || trigger.isupdate){
    for(AC_Trial__c objACT: trigger.new){
        if (objACT.Account__c != null) {
            set_ACTrial.add(objACT.Account__c);
        }
    }
    }
    if(Trigger.isDelete){
        for(AC_Trial__c objACT: Trigger.old){
            if (objACT.Account__c != null) {
                set_ACTrial.add(objACT.Account__c);
            }
        }
    }
    Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Id, AC_Trial_Documents__c FROM Account WHERE Id IN :set_ACTrial]);
	for (Id acctId : accountsMap.keyset()) {
        accountsMap.get(acctId).AC_Trial_Documents__c = 0;
    }
	
    List<AggregateResult> ars = [SELECT Account__c, SUM(Number_Of_Documents__c) FROM AC_Trial__c WHERE Account__c IN :set_ACTrial GROUP BY Account__c];
    
    for (AggregateResult ar : ars) {
        accountsMap.get(String.valueOf(ar.get('Account__c'))).AC_Trial_Documents__c = Integer.valueOf(ar.get('expr0'));
    }
    update accountsMap.values();
}

 
This was selected as the best answer
Jim MontgomeryJim Montgomery
Thank you!!!!