You need to sign in to do that
Don't have an account?
Jim 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;
}
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;
}
All Answers
I trust you are doing very well.
Please try below code:
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, I am getting an error Total_Lease_Area_SF_from_Childern does not exist.
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
Here is the updated trigger.
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();
}