0

I'm trying to update the lookup field (Account ID) using the trigger after inserting the Case record by using the Web-to-Case form. But it is throwing the error "FATAL_ERROR|System.FinalException: Record is read-only". I was tried to insert the data with Lookup field by using Web-to-Case but unfortunately it doesn't work. So i would like to try workaround and passing the Account id after insert event in trigger but failed.Please help me out resolving the issue.

trigger code:

public static void afterInsert(List<Case> newCases, Map<Id, Case> newCaseMap) {
    
    List<Case> newCaseLst = new List<Case>();
                for(Case cse : newCases){
                    cse.AccountId='001XXXXXXXX';
                    //cse.contactId='003XXXXXXXXX';
                    newCaseLst .add(cse);                        
                   
            } 
            update newCaseList1 ;
}

1 Answer 1

1

This is because you are in an after-insert trigger and the records are read-only in that context as they have been written, but not committed, to the database.

To avoid this error you can use a before trigger, which allows you to make changes to the records before they are saved to the database. This is the preferred method when you want to update fields on the records that fired the trigger.

If you want to use an only-after trigger, you will need to create a new instance of the object with the updated values and perform an update DML operation on this new list.

Here is the updated code:

public static void afterInsert(List<Case> newCases, Map<Id, Case> newCaseMap) {    
        List<Case> newCaseList = new List<Case>();
        for(Case cse : newCases){
                Case newCase = new Case(Id = cse.Id);
                newCase.AccountId='001XXXXXXXX';
                newCaseList.add(newCase);                        
           
        } 
        update newCaseList;
}
4
  • 1
    This answer is correct but they should be performing this logic in the before insert. While you can do it in after you really shouldn't. Especially given this example of changing the account. Also don't hardcore IDs as this will cause you technical debt. Commented May 21 at 6:56
  • 1
    @BenjaminPirih The hardcoded ID is just a copy-paste mistake. In most cases, I'd agree with your assertion that this really should be a "before insert" trigger, though web-to-case resolves some of the incoming data only in the after insert context, so Contact ID/Account ID should be considered unreliable until then. As is common in Salesforce, there are exceptions to almost every rule.
    – sfdcfox
    Commented May 21 at 10:08
  • Thanks @sfdcfox. That is a very interesting gotcha when using web-to-case. Had to do a little bit of searching to find this article which explains what you are highlighting above. Any idea on what would happen if they did the assignment on before insert trigger? Seeing this article I would suspect that the before insert account assignment would be overwritten by the database action. As you say this is definitely an exception to the rule. Thanks for enlightening me about this. help.salesforce.com/s/articleView?id=000387008&type=1 Commented Jun 5 at 7:00
  • 1
    @BenjaminPirih Assignment rules run after both before and after triggers (see Triggers and Order of Execution). As such, if one needs to make an assignment, they need an even later update, such as a Record-Triggered Flow with a recursive update.
    – sfdcfox
    Commented Jun 5 at 13:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .