ShowAll Questionssorted byDate Posted 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){
}
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;
}
update list_Account;
} Best Answer chosen by Jim Montgomery Steven 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) {
}
}
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
if (objACT.Account__c != null) {
}
}
}
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();
}``` Khan Anas (Salesforce Developers) Hi Jim,

I trust you are doing very well.

```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){
}

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;
}
}
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 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){
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
}
}
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 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 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){
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
}
}
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 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){
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
}
}
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 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 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) {
}
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
if (objACT.Account__c != null) {
}
}
}
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 Montgomery
Steven, tried the latest code. No error but it doesn't update the account. Jim 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) {
}
}
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
if (objACT.Account__c != null) {
}
}
}
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 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) {
}
}
}
if(Trigger.isDelete){
for(AC_Trial__c objACT: Trigger.old){
if (objACT.Account__c != null) {
}
}
}
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 Montgomery
Thank you!!!!
{"ft_lang_label":"__label__en","ft_lang_prob":0.66430503,"math_prob":0.7109143,"size":3185,"snap":"2021-31-2021-39","text_gpt3_token_len":863,"char_repetition_ratio":0.18264696,"word_repetition_ratio":0.3116883,"special_character_ratio":0.2533752,"punctuation_ratio":0.21157324,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9543869,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34],"im_url_duplicate_count":[null,null,null,null,null,null,null,5,null,1,null,null,null,null,null,5,null,null,null,5,null,1,null,null,null,5,null,null,null,null,null,5,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-02T21:15:46Z\",\"WARC-Record-ID\":\"<urn:uuid:345e3421-79ca-4a2c-9413-a5c3ecb32c88>\",\"Content-Length\":\"241889\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a693679a-e400-4d69-89f7-b63ec859dc66>\",\"WARC-Concurrent-To\":\"<urn:uuid:2dc78f8e-4f3a-4458-8f16-adff602d5f71>\",\"WARC-IP-Address\":\"151.101.201.145\",\"WARC-Target-URI\":\"https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005p2sQAA\",\"WARC-Payload-Digest\":\"sha1:UXO7TQTDTPOMAR2LHWQKU36DL7BVZIBG\",\"WARC-Block-Digest\":\"sha1:UKHZ6OFM4GJIAUL7ZHGUK4P7CZCQWGJX\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154385.24_warc_CC-MAIN-20210802203434-20210802233434-00113.warc.gz\"}"}