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
ViciaVicia 

System.FinalException: SObject row does not allow errors

trigger OnAccount_ConfirmToChange on Account (before update) {
      List<Account> accList = [select Id,Name,(select AccountId,Name from Contacts) from account where Id =                   :Trigger.newMap.keySet()];
// Map<Id,Contact> conMap = new Map<Id,Contact>([select Id from Contact where AccountId in: accList.Id]);
     List<Contact> conList = [select Name,isAccountChanged__c from Contact where AccountId in: accList];
     for(Account acc:accList){
         for(Contact con:conList){
             if(conList.size() > 0){
                 if(con.isAccountChanged__c == false){
                 Account ar = Trigger.oldMap.get(acc.Id);
                 ar.addError('Only if the isAccountChanged is checked,You can modify the account!');
                }
           }
       }
    }
}

Hi, as the code abrove. I want to add a error message on updated account when its contact's field isAccountChanged__c  is false.But I get the error "System.FinalException: SObject row does not allow errors" ,could you help me?

Santosh KumbarSantosh Kumbar

Do you want to add error only if account's all contacts has isAccountChanged__c is false?? or if any one has then add error.?

 

 

Regards

Santosh

www.santoshkumbar.com 

Anoop AsokAnoop Asok

What about using Trigger.NewMap to get the Account record instead of oldMap?

 

Thanks,

Anoop

Jia HuJia Hu

try the code below, it works for me. Add error in the trigger should be added to what is fired.

 

trigger OnAccount_ConfirmToChangeNew on Account (before update) {
for (Account a : Trigger.new) {
for(Contact con:[select AccountId,Name,isAccountChanged__c from Contact where AccountId =:a.Id]) {
if(con.isAccountChanged__c == false){
a.addError('Only if the isAccountChanged is checked,You can modify the account!');
}
}
}
}

sfdcfoxsfdcfox

Jia Hu, your code is not bulkified (SOQL inside loop).

 

It appears the most efficient I could come up with is as follows:

 

trigger OnAccount_ConfirmToChange on Account (before update) {
    for(AggregateResult c:[select accountid Id from contact
                           where accountid in :trigger.new
                           and isaccountchanged__c = false]) {
        Trigger.newMap.get((Id)c.get('Id')).addError(
            'You can only modify the account when Account Change is true!');
    }
}

If any contacts on the account have isaccountchanged__c set to false, the error will trigger. This reduces script complexity, lines of code executed, and query count.

Jia HuJia Hu
Hi sfdcfox,

Thanks for your update.