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
jcookejcooke 

Apex Account Trigger Error need help!

We set up a new account trigger in salesforce today for our parature integration and received this error on the opportunity record changing a record type and then pressing save this error appeared.

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateContact caused an unexpected exception, contact your administrator: UpdateContact: execution of AfterUpdate caused by: System.Exception: Too many DML rows: 112: Trigger.UpdateContact: line 11, column 13".

Click here to return to the previous page.

 

Account apex trigger

BodyDownload Apex
trigger UpdateContact on Account (after insert, after update)
{

    for (Account account : Trigger.new)
    {
        List<Contact> contacts = new List<Contact>();
        contacts = [SELECT Id FROM Contact WHERE AccountId = :account.ID];
        
        for(Contact c: contacts)
        {
            update contacts;
        }
    }
}

How can we fix this? Also strange happening on changing opportunity record type and saving.

jhenningjhenning
What is the trigger supposed to be doing?
jcookejcooke

This Parature/SFDC integration  we were working on only picked up records that have been modified, so changing the Account record won’t trigger the integration to update the related Contacts by default (since nothing was changed on the Contact record) which we wanted to occur.  So this trigger will do a “save” operation on all related Contacts making it look like the record was “modified” but not actually change any data. 

 

This creates a trigger that occurs when an account is saved that will “update” all associated customers without actually making any changes.

For example changing one role to another in a contact record.

 

What is strange this seems to happen on one particular account on the opportunity record adding any products as well that's why we cant change the record type? I set up another opportunity with same product and record type and was fine, but for this account in particular we noticed some contacts were being updated during the integration not sure if that triggered it. We turned off the integration around 2:30pm, but still cannot change this particular opportunity record type due to the product for some reason?

 

jhenningjhenning

I see a couple of problems with the trigger. 1) Within the second For Loop, you are updating contacts instead of c. 2) When you have a DML statement within a For Loop, the best practice within Salesforce is to use a List Variable. you are probably hitting a governor limit. Try this code below:

 

 

trigger UpdateContact on Account (after insert, after update) { for (Account account : Trigger.new) { for(List<Contact> contacts:[SELECT Id FROM Contact WHERE AccountId = :account.ID]) { update contacts; } } }

 

 

 

jhenningjhenning

Here's another version that might be even better:

 

 

trigger UpdateContact on Account (after insert, after update) { List<Account> accounts = Trigger.new; for(List<Contact> contacts:[SELECT Id FROM Contact WHERE AccountId in :accounts]) { update contacts; } }

 

 

 

KkellKkell
Can you please let us know how this trigger would touch the product codes on one particular recordWe could add the same product on another account and it was okThanks!
KkellKkell
Can you please tell us how to remove a trigger using the Eclipse application?
KkellKkell

We are receiving messages from our SF users saying that no one can add products?  Can you please tell us how this trigger would be affect products?

THanks!

jhenningjhenning
Not sure how a trigger on the Account could be affecting Product inserts.
22Mel2222Mel22

Hi John, I am very new to triggers and am trying to modify the code you provided below. I need to change the "Contact Status" to "Inactive" (custom field on the Contact record) for all Accounts that have an "Account Status" of "Inactive" (custom field on the Account record). 

 

Is this easily done? Any help would be appreciated! Thank you!!!