3

I have a custom CB field on the contact record called connect_america_account__c. When the value of this contact field is true, I would like the CB field value on the related account, also called (connect_america_account__c) to also be true. Here is the code I've constructed:

trigger ConnectA on Contact  (after insert,after update) {

    List<Id> AccID = New List<Id>();

    for(Contact con : Trigger.new){
        if(con.connect_America_Account__c = true && con.AccountId != null){
            AccID.add(con.AccountId);
        }
    }

    List<Account> accList = [SELECT Name, BillingStreet FROM Account WHERE id in :AccID];

    for(Integer i = 0; i < accList.size(); i++){
        accList[i].connect_America_Account__c = true;
    }

    update accList;
}

Here is the error message:

Error:Apex trigger ConnectA caused an unexpected exception, contact your administrator: ConnectA: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.ConnectA: line 9, column 1

  • That error commonly happens when you try to update the data from e.g. Trigger.new. Line 9 in the code currently posted is not somewhere that an error could occur. Can you make sure you post exactly the code and error message that go together? Also con.connect_America_Account__c = True is always true; con.connect_America_Account__c == True is probably what you meant or better just con.connect_America_Account__c. – Keith C Jun 18 '14 at 12:39
  • The error is what the end users sees.So I change the trigger to fire after insert, and the user is able to save the data, but the trigger is not executing properly. – Cheryl815 Jun 18 '14 at 16:22
1

Is there a reason you are not using Process Builder for this? It would be a great use case for it. Set the Process Builder to fire off when the Contact's "connect_america_account__c" = TRUE. If TRUE, do a Record Update on the AccountId associated to the Contact. Pretty simple problem that you have... so simple you dont need code!

0

I know its too late. But hope it helps someone:

trigger ConnectA on contact(after insert, after update){
    list<contact> con = trigger.new;
    list<Id> accIdList = new list<id>();
    for(contact c: con){
        if(c.connect_America_Account__c == true && c.AccountId != null){
            accIdList.add(c.accountId);
        }
    }
    list<account> acc = [SELECT id, connect_America_Account__c from Account WHERE Id =: accIdList];
    for(Account a: acc){
        a.connect_America_Account__c = true;
    }
    update acc;
}
-1

Read-only means that you can NOT change the value of the fields. Records that fire an "After" trigger are read-only. You need to change this to a "Before" trigger for it to work. Something like this...

trigger ConnectA on Contact  (before insert,before update) {
     // loop through new contact values
     for(Contact con : Trigger.new){
           // get old contact value
          Contact contactOld = System.Trigger.oldMap.get(con.id);
          // check if field was changed from false to true
          if (contactOld.connect_America_Account__c==false){
              if (con.connect_America_Account__c==true){
                    // update other field to true
                    if (con.accountID!=null)
                           Account acct = [SELECT Name, BillingStreet FROM Account WHERE id=:con.accountId];
                    acct.connect_America_Account__c=true;
              }
          }
     }
}
  • this won't work -- Trigger.oldMap is not available in before insert context; you have select statements inside of for loops – cropredy Dec 6 '14 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.